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.
classNode:def__init__(self):self.char={}self.is_end=FalseclassTrie:def__init__(self):self.root=Node()definsert(self,word:str)->bool:# Return True if inserted; False if word is a prefix or has a prefixcurr_node=self.rootforcinword:ifcnotincurr_node.char:curr_node.char[c]=Node()curr_node=curr_node.char[c]ifcurr_node.is_end:# An existing word is a prefix of the current wordreturnFalseifcurr_node.char:# The current word is a prefix of an existing wordreturnFalsecurr_node.is_end=True# The word was successfully insertedreturnTruedefnoPrefix(words:list[str])->None:trie=Trie()forwordinwords:ifnottrie.insert(word):print('BADSET')print(word)returnprint('GOODSET')
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
No Prefix Set
You are viewing a single comment's thread. Return to all comments →