class TrieNode { children: Map<string, TrieNode> = new Map(); isEnd: boolean = false; } class Trie { root: TrieNode = new TrieNode(); insert(word: string): void { let node = this.root; for (const char of word) { if (!node.children.has(char)) { node.children.set(char, new TrieNode()); } node = node.children.get(char)!; } node.isEnd = true; } longestWord(): string { let result = ""; const dfs = (node: TrieNode, path: string): void => { if (path.length > result.length || (path.length === result.length && path < result)) { result = path; } for (const [char, child] of [...node.children.entries()].sort((a, b) => a[0].localeCompare(b[0]))) { if (child.isEnd) { dfs(child, path + char); } } }; dfs(this.root, ""); return result; } } const words = ["a", "app", "ap", "apply", "apple", "appl", "b", "ban", "banana"]; const trie = new Trie(); for (const w of words) { trie.insert(w); } console.log(trie.longestWord());
The code builds a Trie and searches for the longest word such that every prefix of the word is also a word in the list. Among the given words, "apple" has prefixes "a", "ap", "app", "appl" all present. "apply" is not valid because "appl" is present but "apply" is not marked as end in the Trie (not inserted as a complete word). "banana" is longer but prefixes like "banan" are missing.
The 'isEnd' boolean indicates that the path from the root to this node forms a complete word in the dictionary. This helps in checking if prefixes are valid words.
class TrieNode { children: Map<string, TrieNode> = new Map(); isEnd: boolean = false; } class Trie { root: TrieNode = new TrieNode(); insert(word: string): void { let node = this.root; for (const char of word) { if (!node.children.has(char)) { node.children.set(char, new TrieNode()); } node = node.children.get(char)!; } node.isEnd = true; } longestWord(): string { let result = ""; const dfs = (node: TrieNode, path: string): void => { if (path.length > result.length || (path.length === result.length && path < result)) { result = path; } for (const [char, child] of node.children.entries()) { if (child.isEnd) { dfs(child, path + char); } } }; dfs(this.root, ""); return result; } } const words = ["a", "app", "ap", "apply", "apple", "appl"]; const trie = new Trie(); for (const w of words) { trie.insert(w); } console.log(trie.longestWord());
The DFS starts from the root node which has isEnd = false. Since the DFS only continues down children nodes that have isEnd = true, and the root is not marked as a word, the initial call does not update the result. The root should be considered as a valid starting point or marked as isEnd = true to allow traversal.
class TrieNode { children: Map<string, TrieNode> = new Map(); isEnd: boolean = false; } class Trie { root: TrieNode = new TrieNode(); insert(word: string): void { let node = this.root; for (const char of word) { if (!node.children.has(char)) { node.children.set(char, new TrieNode()); } node = node.children.get(char)!; } node.isEnd = true; } longestWord(): string { let result = ""; const dfs = (node: TrieNode, path: string): void => { if (path.length > result.length || (path.length === result.length && path < result)) { result = path; } for (const [char, child] of [...node.children.entries()].sort()) { if (child.isEnd) { dfs(child, path + char); } } }; dfs(this.root, ""); return result; } }
Calling sort() on an array of Map entries without a compare function is valid syntax but may not sort as expected. However, in TypeScript, sorting Map entries requires a compare function to avoid errors. The other options are valid syntax: semicolons are optional, arrow functions can omit return type, and '!' is a valid non-null assertion operator.
const words = ["a", "ap", "app", "appl", "apple", "apply", "b", "ban", "bana", "banan", "banana"]; // Assume Trie insertion and longest word logic as usual.
Words "apple" and "banana" both have all prefixes present in the list. "apply" is invalid because "appl" is present but "apply" is not marked as a complete word in the dictionary (assuming it is). Here, "apply" is present, but "appl" is also present, so "apply" qualifies. So the longest words with all prefixes are "apple" and "banana" (both length 5 and 6 respectively). So count is 2.