We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
my solution in python, please comment if you think it can be even faster, sharing is caring
classTrieNode:__slots__='children','numberofwordsafter'def__init__(self):self.children={}self.numberofwordsafter=0classTrie:def__init__(self):self.root=TrieNode()definsert(self,word:str):currentNode=self.rootcurrentNode.numberofwordsafter+=1forcinword:ifcnotincurrentNode.children:currentNode.children[c]=TrieNode()currentNode=currentNode.children[c]currentNode.numberofwordsafter+=1defnumwords(self,word):cur_node=self.rootforcinword:ifcnotincur_node.children:return0else:cur_node=cur_node.children[c]returncur_node.numberofwordsafterdefcontacts(queries):# Write your code herenames=Trie()results=[]forqinqueries:ifq[0]=='add':names.insert(q[1])else:res=names.numwords(q[1])results.append(res)returnresults
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Contacts
You are viewing a single comment's thread. Return to all comments →
my solution in python, please comment if you think it can be even faster, sharing is caring