-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindLongestWords.js
56 lines (40 loc) · 1.81 KB
/
findLongestWords.js
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
// Google Interview Question
// Please use this Google doc during your interview (your interviewer will see what you write here). To free your hands for typing, we recommend using a headset or speakerphone.
// 15 mins
// Given a word list and a list of letters, design a program find the longest words that can be constructed from those letters, using each letter only once.
// O(n^2) - Trie O(n) would be great -
// Presort by length - O(longN)
const findLongestWords = (dictionaryArr, lettersArr) => {
let longestWords = [];
let lettersArrFreq = [];
let dictionaryArrCharFreq = [];
// construct an array of letter frequencies for each
for(let i=0; i<lettersArr.length; i++) {
let chache = {}; {a: 1, b:4, c:5 …}
const letters = lettersArr[i].join(‘’);
lettersArr.forEach((letter) => {
cache[letter] = cache[letter] ? 0 : cache[letter]++;
});
lettersArrFreq.push(chache);
}
// Optimization by presorting the dictionary by length
for(let i=0; i<dictionaryArr.length; i++) {
let chache = {};
const wordArr = lettersArr[i].split(“”);
wordArr.forEach((letter) => {
cache[letter] = cache[letter] ? 0 : cache[letter]++;
});
chache.word = lettersArr[i];
dictionaryArrCharFreq.push(chache);
}
lettersArrFreq.forEach((letters) => {
dictionaryArrCharFreq.forEach((word) => {
lettersFreqTuple = Object.keys(letters)
dictionaryFreqTuple = Object.keys(word)
// loop through freq arrays to check that the freqs are the same for each tuple
// if they are the same then we push it to the longestWords arr
});
});
return longestWords;
}
findLongestWords(dictionary, ‘atc’) // CAT