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.
classTrieNode:def__init__(self):self.children={}self.is_end_of_word=FalseclassTrie:def__init__(self):self.root=TrieNode()definsert(self,word):# return true if inserted# return false if word either is a prefix of other words# or has other word as its prefix so farcurr_node=self.rootforcinword:ifcnotincurr_node.children:curr_node.children[c]=TrieNode()curr_node=curr_node.children[c]# has other word as its prefix so farifcurr_node.is_end_of_word:returnFalse# end of a word markercurr_node.is_end_of_word=True# is a prefix of other words if it has childreturnnotcurr_node.childrendefnoPrefix(words):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 →