classTrie:def__init__(self):self.children=[None]*26self.v=self.pv=0definsert(self,word:str)->None:node=selfforcinword:idx=ord(c)-ord('a')ifnode.children[idx]isNone:node.children[idx]=Trie()node=node.children[idx]node.pv+=1node.v+=1defcountWordsEqualTo(self,word:str)->int:node=self.search(word)return0ifnodeisNoneelsenode.vdefcountWordsStartingWith(self,prefix:str)->int:node=self.search(prefix)return0ifnodeisNoneelsenode.pvdeferase(self,word:str)->None:node=selfforcinword:idx=ord(c)-ord('a')node=node.children[idx]node.pv-=1node.v-=1defsearch(self,word):node=selfforcinword:idx=ord(c)-ord('a')ifnode.children[idx]isNone:returnNonenode=node.children[idx]returnnode# Your Trie object will be instantiated and called as such:# obj = Trie()# obj.insert(word)# param_2 = obj.countWordsEqualTo(word)# param_3 = obj.countWordsStartingWith(prefix)# obj.erase(word)
classTrie{privateTrie[]children=newTrie[26];privateintv;privateintpv;publicTrie(){}publicvoidinsert(Stringword){Trienode=this;for(charc:word.toCharArray()){c-='a';if(node.children[c]==null){node.children[c]=newTrie();}node=node.children[c];++node.pv;}++node.v;}publicintcountWordsEqualTo(Stringword){Trienode=search(word);returnnode==null?0:node.v;}publicintcountWordsStartingWith(Stringprefix){Trienode=search(prefix);returnnode==null?0:node.pv;}publicvoiderase(Stringword){Trienode=this;for(charc:word.toCharArray()){c-='a';node=node.children[c];--node.pv;}--node.v;}privateTriesearch(Stringword){Trienode=this;for(charc:word.toCharArray()){c-='a';if(node.children[c]==null){returnnull;}node=node.children[c];}returnnode;}}/** * 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{public:Trie():children(26),v(0),pv(0){}voidinsert(stringword){Trie*node=this;for(charc:word){c-='a';if(!node->children[c]){node->children[c]=newTrie();}node=node->children[c];++node->pv;}++node->v;}intcountWordsEqualTo(stringword){Trie*node=search(word);returnnode?node->v:0;}intcountWordsStartingWith(stringprefix){Trie*node=search(prefix);returnnode?node->pv:0;}voiderase(stringword){Trie*node=this;for(charc:word){c-='a';node=node->children[c];--node->pv;}--node->v;}private:vector<Trie*>children;intv,pv;Trie*search(string&word){Trie*node=this;for(charc:word){c-='a';if(!node->children[c]){returnnullptr;}node=node->children[c];}returnnode;}};/** * 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); */
typeTriestruct{children[26]*Trievintpvint}funcConstructor()(_Trie){return}func(this*Trie)Insert(wordstring){node:=thisfor_,c:=rangeword{c-='a'ifnode.children[c]==nil{node.children[c]=&Trie{}}node=node.children[c]node.pv++}node.v++}func(this*Trie)CountWordsEqualTo(wordstring)int{node:=this.search(word)ifnode==nil{return0}returnnode.v}func(this*Trie)CountWordsStartingWith(prefixstring)int{node:=this.search(prefix)ifnode==nil{return0}returnnode.pv}func(this*Trie)Erase(wordstring){node:=thisfor_,c:=rangeword{c-='a'node=node.children[c]node.pv--}node.v--}func(this*Trie)search(wordstring)*Trie{node:=thisfor_,c:=rangeword{c-='a'ifnode.children[c]==nil{returnnil}node=node.children[c]}returnnode}/** * Your Trie object will be instantiated and called as such: * obj := Constructor(); * obj.Insert(word); * param_2 := obj.CountWordsEqualTo(word); * param_3 := obj.CountWordsStartingWith(prefix); * obj.Erase(word); */