by Pawan Kumar @jsartisan
Implement a prefix tree (trie) that supports string insertion and search operations.
The PrefixTree
class should support:
insert(word)
: Adds a word to the triesearch(word)
: Returns true if word exists in triestartsWith(prefix)
: Returns true if any word starts with prefix
Constraints:
- 1 ≤ word.length, prefix.length ≤ 1000
- All strings contain only lowercase English letters
Examples:
const trie = new PrefixTree();
trie.insert("dog");
trie.search("dog"); // returns true
trie.search("do"); // returns false
trie.startsWith("do"); // returns true
trie.insert("do");
trie.search("do"); // returns true