Implement Trie (Prefix Tree)
MediumTrees
Decode it first. Clues, constraints, pattern and plan before any code.A trie (say "try", also called a prefix tree) stores strings one character per level, so words that share a beginning share a path. It is what powers autocomplete and spell checkers.
Build a class Trie with:
insert(word): addwordto the trie.search(word): returnTrueifwordwas inserted as a whole word.starts_with(prefix)(startsWithin TypeScript): returnTrueif any inserted word begins withprefix.
trie = Trie()
trie.insert("apple")
trie.search("apple") → True
trie.search("app") → False (only a prefix so far)
trie.starts_with("app") → True
trie.insert("app")
trie.search("app") → TrueConstraints
1 <= word.length, prefix.length <= 2000wordandprefixconsist only of lowercase English letters.- At most
3 * 10^4calls in total toinsert,searchandstarts_with.