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.
for word in words:
current_node = trie.root
for char in word:
i = alphabet.index(char)
if current_node.children[i] is None:
current_node.children[i] = TrieNode()
current_node = current_node.children[i]
if current_node.is_terminal:
print('BAD SET')
print(word)
return
current_node.is_terminal = True
for child in current_node.children:
if child is not None:
print('BAD SET')
print(word)
return
print('GOOD SET')
return
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 →
class TrieNode: def init(self): self.is_terminal = False self.children = [None] * 10 #alphabet ends at j
class Trie: def init(self): self.root = TrieNode()
def noPrefix(words): trie = Trie() alphabet = string.ascii_lowercase