No Prefix Set

  • + 0 comments

    The testcases are broken in this

    For test case 2

    Correct answer : beeheedjacieefdd Given Answer: ej

    class TrieNode:
        def __init__(self, data, is_word_end):
            self.data = data
            self.is_word_end = is_word_end
            self.children = {}
            
    def add_word_to_trie(word, root):
        if len(word) <= 0:
            root.is_word_end = True
            return
        
        first_char = word[0]
        new_root = root.children.get(first_char, TrieNode(first_char, False))
        root.children[first_char] = new_root
                
        return add_word_to_trie(word[1:], new_root)
        
    def has_prefix(word, root):
        if len(word) == 0:
            return False
        
        if root.is_word_end:
            return True
            
        next_root = root.children[word[0]]
        return has_prefix(word[1:], next_root)
        
    def noPrefix(words):
        root = TrieNode('*', False)
        words_dict = {}
        for i in range(len(words)):
            add_word_to_trie(words[i], root)
            words_dict[words[i]] = words_dict.get(words[i], 0) + 1
        
        for i in range(len(words)):
            if has_prefix(words[i], root) or words_dict[words[i]] > 1:
                print('BAD SET')
                print(words[i])
                return
    
        print('GOOD SET')