Longest Word in Dictionary LeetCode solution in JavaScript

1
var longestWord = function (words) {
2
words.sort();
3
let trie = new Trie();
4
let result = "";
5

6
for (const word of words) {
7
if (word.length === 1) {
8
trie.insert(word);
9
result = word.length > result.length ? word : result;
10
} else {
11
let has = trie.search(word.slice(0, word.length - 1));
12
if (has) {
13
trie.insert(word);
14
result = word.length > result.length ? word : result;
15
}
16
}
17
}
18

19
return result;
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0