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.
I get a pass on 12/13 cases, even some of 100000 operations, but case #12 crashes (not time limit exceeded, but runtime error). I've checked all 13 cases myself in my computer, and all of them work, no infinite recursion, invalid accesses or anything, so it seems its on the side of memory consumption on the judge side for Python. I don't know where else to reduce space :-(
classTrie(object):def__init__(self):# The children of this node.self.children={}# The amount of all words this node acts as# a prefix for.self.count=0defadd(self,string,index):# Increase the count of childrens for this # character. At every insertion, when it goes # through this node, it will increase the value, # keeping track of all the children downwards.self.count+=1# If there's still characters left to insert.ifindex<len(string):char=string[index]# If there's currently not a children with a # given char.ifnotself.children.get(char):self.children[char]=Trie()child=self.children[char]# After creating/retrieving the children, call # add on it and advance the index interator of # the string.child.add(string,index+1)deffind(self,string,index):# If there's still characters to match.ifindex<len(string):# If there's a node with the current character.ifself.children.get(string[index]):child=self.children[string[index]]# Go down recursively to it advancing the # string index.returnchild.find(string,index+1)else:return0# When all characters in query have been matched.else:returnself.countn=int(input())trie=Trie()for_inrange(n):op,word=input().split()ifop=="add":trie.add(word,0)ifop=="find":count=trie.find(word,0)print(count)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tries: Contacts
You are viewing a single comment's thread. Return to all comments →
I get a pass on 12/13 cases, even some of 100000 operations, but case #12 crashes (not time limit exceeded, but runtime error). I've checked all 13 cases myself in my computer, and all of them work, no infinite recursion, invalid accesses or anything, so it seems its on the side of memory consumption on the judge side for Python. I don't know where else to reduce space :-(