No Prefix Set

  • + 0 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

    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