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,val:Optional[str]=None):self.val=valself.endOfWord=Falseself.children={}classTrie:def__init__(self):self.root=TrieNode()defadd_new_word(self,word:str)->bool:cur=self.rootfori,charinenumerate(word):# next letter is already under root and marks end of wordifcharincur.childrenand(i==len(word)-1orcur.children[char].endOfWord):returnFalsenext_node=cur.children.setdefault(char,TrieNode(char))cur=next_nodeifcur.endOfWord:returnFalsecur.endOfWord=TruereturnTruedefnoPrefix(words)->bool:trie=Trie()forwordinwords:ifnottrie.add_new_word(word):print("BAD SET")print(word)returnFalseprint("GOOD SET")returnTruenoPrefix(words)
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 →
Solution in Python, a little rusty on perfect Trie implementation but this did the trick.
To tackle the ordering and avoid sorting the list, the key check is in
Full code: