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.
you can do a neat one-line trie in python using defaultdict which is pretty cool:
fromsysimportstdinfromcollectionsimportdefaultdictdefTrie():returndefaultdict(Trie)defadd(trie,word):ifword=='':iflen(trie)>0:#currentwordisaprefixofapreviouswordreturnFalsetrie[word]=''returnTrueif''intrie:# a prefix of the current word is a previous wordreturnFalsereturnadd(trie[word[0]],word[1:])stdin.readline()trie=Trie()forlineinstdin:line=line.strip()ifnotadd(trie,line):print"BAD SET"printlinesys.exit()print"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 →
you can do a neat one-line trie in python using defaultdict which is pretty cool: