No Prefix Set

  • + 0 comments
    class Node():
        def __init__(self, char):
            self.char = char
            self.children = dict()
            self.end = False
    
    def noPrefix(words):
        trie = Node("")
        for i in range(len(words)):
            word = words[i]
            head = trie
            for i in range(len(word)):
                char = word[i]
                new_head = head.children.get(char)
                if new_head:
                    if i == len(word) -1 or new_head.end:
                        print("BAD SET")
                        print(word)
                        exit()
                    head = new_head
                else:
                    head.children[char] = Node(char)
                    head = head.children[char]
                    if i == len(word) -1:
                        head.end = True
        print("GOOD SET")