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 Python 3 attempt by incrementally inserting words into a trie (thanks to inspiration from many of you guys) (must be some other "cleaner" way to do the create dict if not exists step, but I prefer readability in my own terms XD):
defnoPrefix(words):#Write your code here_end="_end"trie={}#return the current word where the check failsdefbad(s):print("BAD SET")print(s)#incremental insertion of wordsforwordinwords:current=trie#for each word, insert and change current map/dict per letterforcinword:#FAIL: a processed word is the prefix of the current wordif_endincurrent:bad(word)return#create map/dict if missingifcnotincurrent:current[c]={}current=current[c]#FAIL: current word map/dict is not empty#meaning either:#- current word was already inserted in the trie#- current word was a prefix of previously inserted wordsiflen(current)>0:bad(word)return#mark the current node where the current word ends herecurrent[_end]=_endprint("GOOD SET")
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 →
my Python 3 attempt by incrementally inserting words into a trie (thanks to inspiration from many of you guys) (must be some other "cleaner" way to do the create dict if not exists step, but I prefer readability in my own terms XD):
edit: simplified and updated thanks to @dgzwiro !