A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
Trie() Initializes the trie object.
void insert(String word) Inserts the string word into the trie.
int countWordsEqualTo(String word) Returns the number of instances of the string word in the trie.
int countWordsStartingWith(String prefix) Returns the number of strings in the trie that have the string prefix as a prefix.
void erase(String word) Erases the string word from the trie.
Trietrie=newTrie();trie.insert("apple");// Inserts "apple".trie.insert("apple");// Inserts another "apple".trie.countWordsEqualTo("apple");// There are two instances of "apple" so return 2.trie.countWordsStartingWith("app");// "app" is a prefix of "apple" so return 2.trie.erase("apple");// Erases one "apple".trie.countWordsEqualTo("apple");// Now there is only one instance of "apple" so return 1.trie.countWordsStartingWith("app");// return 1trie.erase("apple");// Erases "apple". Now the trie is empty.trie.countWordsStartingWith("app");// return 0
Constraints:
1 <= word.length, prefix.length <= 2000
word and prefix consist only of lowercase English letters.
At most 3 * 10^4 calls in total will be made to insert, countWordsEqualTo, countWordsStartingWith, and erase.
It is guaranteed that for any function call to erase, the string word will exist in the trie.
structTrieNode{TrieNode*nexts[26];intpassing=0;intending=0;~TrieNode(){for(auto&node:nexts){deletenode;}}};classTrie{public:TrieNode*root;Trie(){root=newTrieNode();}~Trie(){deleteroot;}voidinsert(stringword){TrieNode*node=root;for(constchar&ch:word){intindex=ch-'a';if(!node->nexts[index]){node->nexts[index]=newTrieNode();}node=node->nexts[index];node->passing++;}node->ending++;}intcountWordsEqualTo(stringword){TrieNode*node=root;for(constchar&ch:word){intindex=ch-'a';if(!node->nexts[index]){return0;}node=node->nexts[index];}returnnode->ending;}intcountWordsStartingWith(stringprefix){TrieNode*node=root;for(constchar&ch:prefix){intindex=ch-'a';if(!node->nexts[index]){return0;}node=node->nexts[index];}returnnode->passing;}voiderase(stringword){TrieNode*node=root;for(constchar&ch:word){intindex=ch-'a';node=node->nexts[index];node->passing--;}node->ending--;}};/** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * int param_2 = obj->countWordsEqualTo(word); * int param_3 = obj->countWordsStartingWith(prefix); * obj->erase(word); */
Time and space complexity of each function:
Constructor (Trie()):
Time Complexity: . The constructor only initializes the root, which takes constant time.
Space Complexity: for the allocation of the root node.
Insert (insert(string word)):
Time Complexity: , where is the length of the word being inserted. This is because it traverses the Trie for the length of the word.
Space Complexity: in the worst case when a new path is created for the entire length of the word. This accounts for the space used by the new nodes.
Count Words Equal To (countWordsEqualTo(string word)):
Time Complexity: , where is the length of the word. It traverses the Trie for the length of the word to find the ending count.
Space Complexity: , as it only uses a pointer to traverse the nodes.
Count Words Starting With (countWordsStartingWith(string prefix)):
Time Complexity: , where is the length of the prefix. The method traverses the Trie for the length of the prefix.
Space Complexity: , since it similarly uses a pointer for traversal.
Erase (erase(string word)):
Time Complexity: , where is the length of the word. It decreases the passing count while traversing the Trie.
Space Complexity: , as it utilizes a pointer to navigate the Trie.
voiderase(stringword){if(countWordsEqualTo(word)<=0)return;// unnecessary for this questionTrieNode*node=root;for(constchar&ch:word){intindex=ch-'a';if(--node->nexts[index]->passing==0){node->nexts[index]=nullptr;deletenode->nexts[index];return;}node=node->nexts[index];}node->ending--;}
structTrieNode{unordered_map<char,TrieNode*>nexts;intpassing=0;intending=0;~TrieNode(){for(auto&pair:nexts){deletepair.second;}}};classTrie{public:TrieNode*root;Trie():root(newTrieNode()){}~Trie(){deleteroot;}voidinsert(stringword){autonode=root;node->passing++;for(constauto&ch:word){if(!node->nexts.count(ch)){node->nexts[ch]=newTrieNode();}node=node->nexts[ch];node->passing++;}node->ending++;}intcountWordsEqualTo(stringword){autonode=root;for(constauto&ch:word){if(!node->nexts.count(ch)){return0;}node=node->nexts[ch];}returnnode->ending;}intcountWordsStartingWith(stringprefix){autonode=root;for(constauto&ch:prefix){if(!node->nexts.count(ch)){return0;}node=node->nexts[ch];}returnnode->passing;}voiderase(stringword){autonode=root;node->passing--;for(constauto&ch:word){node=node->nexts[ch];node->passing--;}node->ending--;}};/** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * int param_2 = obj->countWordsEqualTo(word); * int param_3 = obj->countWordsStartingWith(prefix); * obj->erase(word); */
classTrie{private:conststaticintN=31010;intson[N][26]{0};intpassing[N]{0};intending[N]{0};intcnt=0;public:Trie(){}voidinsert(stringword){intcurrent=0;passing[current]++;for(char&ch:word){intindex=ch-'a';if(!son[current][index]){son[current][index]=++cnt;}current=son[current][index];passing[current]++;}ending[current]++;}intcountWordsEqualTo(stringword){intcurrent=0;for(char&ch:word){intindex=ch-'a';if(!son[current][index]){return0;}current=son[current][index];}returnending[current];}intcountWordsStartingWith(stringprefix){intcurrent=0;for(char&ch:prefix){intindex=ch-'a';if(!son[current][index]){return0;}current=son[current][index];}returnpassing[current];}voiderase(stringword){intcurrent=0;passing[current]--;for(char&ch:word){intindex=ch-'a';current=son[current][index];passing[current]--;}ending[current]--;}};/** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * int param_2 = obj->countWordsEqualTo(word); * int param_3 = obj->countWordsStartingWith(prefix); * obj->erase(word); */