Longest Word in Dictionary LeetCode solution in JavaScript
1var longestWord = function (words) {2words.sort();3let trie = new Trie();4let result = "";56for (const word of words) {7if (word.length === 1) {8trie.insert(word);9result = word.length > result.length ? word : result;10} else {11let has = trie.search(word.slice(0, word.length - 1));12if (has) {13trie.insert(word);14result = word.length > result.length ? word : result;15}16}17}1819return result;20};