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 prefixConstraints:
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