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.
very nice solution! I stumbled upon the same infinite defaultdict pattern but used reduce to build the tree. (Arguably assertion shouldn't be used for main control flow but maybe ok for this short exercise.)
fromcollectionsimportdefaultdictdefinfdict():returndefaultdict(infdict)defbuild_trie(node,ch):child_node=node[ch]assertNonenotinchild_node# no existing words are prefixes of this wordreturnchild_noden=int(raw_input())trie=infdict()try:for_inrange(n):s=raw_input()end_node=reduce(build_trie,s,trie)assertnotend_node# not a prefix of an existing wordend_node[None]# None = end-of-word sentinelprint"GOOD SET"exceptAssertionError:print"BAD SET"prints
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 →
very nice solution! I stumbled upon the same infinite defaultdict pattern but used
reduce
to build the tree. (Arguably assertion shouldn't be used for main control flow but maybe ok for this short exercise.)