Implement Trie (Prefix Tree)
MediumP2Coding
Implement a trie (prefix tree) for lower-case words.
Trie()creates an empty trie.insert(word)addswordto the trie.search(word)returnstrueif exactlywordwas inserted earlier.startsWith(prefix)returnstrueif some inserted word begins withprefix.
Example 1
- Input
- ["Trie","insert","search","search","startsWith","insert","search"] [[],["apple"],["apple"],["app"],["app"],["app"],["app"]]
- Output
- [null,null,true,false,true,null,true]
Constraints
- 1 <= word.length, prefix.length <= 2000
- Words consist of lower-case English letters
- At most 3 * 10^4 calls in total
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 2 visible tests, Submit all 6. ⌘Enter runs, ⌘⇧Enter submits.