-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhackerRankAnagrams.js
More file actions
131 lines (117 loc) · 3.2 KB
/
hackerRankAnagrams.js
File metadata and controls
131 lines (117 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// The function is expected to return an INTEGER.
// The function accepts following parameters:
// 1. STRING a
// 2. STRING b
// UPER
// U
// TAKE 2 STRINGS RETURN AN INT FOR HOW MANY CHARS NEED BE REMOVED
// TO MAKE ANAGRAMS
// EX: INPUTS: a = 'cde' b = 'abc' OUTPUT = 4
// FOR THESE TO BE ANAGRAMS FOLLOWING CHARS MUST BE REMOVED: d,e,a,b
// CHARS REMOVED ADD UP TO 4, SO OUTPUT IS 4
// P
// TAKE COUNT OF a + b EX: 6
// ADD A TO ARRAY
// ADD B TO ARRAY IF VALUES NOT IN ARRAY
// COUNT VALUES IN ARRAY
// WONT WORK
// TAKE COUNT OF a + b EX: 6
// FOR EACH CHAR IN a
// SEE IF CHAR IS IN B
// IF SO COUNT + 2
// SUBTRACT COUNT FROM TOTAL CHARS OF A+B
// RETURN RESULT EX: 4
// function makeAnagrams(a, b) {
// // TAKE COUNT OF a + b EX: 6
// const totalChars = a.length + b.length
// console.log(totalChars)
// // FOR EACH CHAR IN a
// var i = a.length
// const aArr = []
// const bArr = []
// while (i--) {
// const aV = a.charAt(i)
// console.log(aV)
// aArr.push(aV)
// }
// console.log(aArr)
// // SEE IF CHAR IS IN B
// var z = b.length
// while (z--) {
// const bV = b.charAt(z)
// console.log(bV)
// aArr.push(bV)
// }
// console.log(aArr)
// // IF SO COUNT + 2
// let sorted_arr = aArr.slice().sort() // You can define the comparing function here.
// // JS by default uses a crappy string compare.
// // (we use slice to clone the array so the
// // original array won't be modified)
// console.log('sorted_arr', sorted_arr)
// let results = []
// for (let i = 0; i < sorted_arr.length - 1; i++) {
// console.log('sortPre', sorted_arr[i + 1])
// if (sorted_arr[i + 1] === sorted_arr[i]) {
// console.log('sortI', sorted_arr[0])
// results.push(sorted_arr[i])
// }
// console.log('results', results)
// return results
// }
// // SUBTRACT COUNT FROM TOTAL CHARS OF A+B
// // RETURN RESULT EX: 4
// console.log(results.length())
// return results.length()
// }
// makeAnagrams('cde', 'abc')
function makeAnagrams(a, b) {
// TAKE COUNT OF a + b EX: 6
const totalChars = a.length + b.length
console.log(totalChars)
// FOR EACH CHAR IN a
var i = a.length
const aArr = []
const bArr = []
const outCount = 0
var masterCount = 0
while (i--) {
const aV = a.charAt(i)
console.log(aV)
aArr.push(aV)
}
console.log(aArr)
// GET CHAR FROM B
var z = b.length
while (z--) {
const bV = b.charAt(z)
console.log(bV)
aArr.push(bV)
}
console.log(aArr)
const uniq = aArr
.map((name) => {
return {
count: 1,
name: name,
}
})
.reduce((a, b) => {
a[b.name] = (a[b.name] || 0) + b.count
console.log('b.count', b.count)
masterCount += 1
console.log('a', a)
return a
}, {})
console.log('masterCount', masterCount)
const duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)
console.log('duplicates', duplicates)
// IF SO COUNT + 2
const dups = duplicates.length * 2
console.log('dups', dups)
const finalAnswer = totalChars - dups
console.log('finalAnswer', finalAnswer)
return dups
}
// makeAnagrams('cde', 'abc') -- passes
makeAnagrams('fcrxzwscanmligyxyvym', 'jxwtrhvujlmrpdoqbisbwhmgpmeoke') // expected 30