Back to DSA
Implement Trie (Prefix Tree)
mediumA trie (prefix tree) is a specialised tree for storing and looking up strings efficiently. Build a Trie class with three operations: insert(word) adds a word, search(word) returns true only if the exact word exists, and startsWith(prefix) returns true if any stored word begins with the given prefix.
Examples
Example 1:
Input:
Trie(), insert('hello'), search('hello'), search('hell'), startsWith('hel'), insert('hell'), search('hell')Output:
[null,null,true,false,true,null,true]Hints
1234567