-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdropbox_hackerrank_spellingbee.js
More file actions
61 lines (49 loc) · 1.48 KB
/
dropbox_hackerrank_spellingbee.js
File metadata and controls
61 lines (49 loc) · 1.48 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
const wordlist = ['APPLE', 'PLEAS', 'PLEASE'];
const puzzles= ['AELWXYZ', 'AELPXYZ', 'AELPSXY', 'SAELPXY', 'XAELPSY'];
//output: [0, 1, 3, 2, 0]
function spellingBeeSolutions(wordlist, puzzles) {
let newArr = [];
for (let i = 0; i < puzzles.length; i++) {
newArr.push(0);
}
for (let i = 0; i < puzzles.length; i++) {
for (let j = 0; j < wordlist.length; j++) {
if (checkWordList(puzzles[i], wordlist[j])) {
newArr[i]++;
}
}
}
return newArr;
}
const checkWordList = (wordlist1, wordlist2) => {
let wordlistObj = {};
let inside = true;
let count = 0;
for (let i = 0; i < wordlist2.length; i++) {
if (wordlistObj[wordlist2[i]]) {
wordlistObj[wordlist2[i]]++;
} else {
wordlistObj[wordlist2[i]] = 1;
}
}
for (let i = 0; i < wordlist2.length; i++) {
if (!wordlist2.includes(wordlist1[i])) {
return false;
}
if (wordlist1.includes(wordlist2[i])) {
for (key in wordlistObj) {
if (key === wordlist2[i]) {
count += wordlistObj[key];
}
}
if (count === wordlist2.length) {
return inside;
}
} else {
inside = false;
break;
}
}
return inside;
}
console.log(spellingBeeSolutions(wordlist, puzzles))