No Prefix Set

Sort by

recency

|

35 Discussions

|

  • + 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")
    
  • + 0 comments

    Java

    	TrieNode root = new TrieNode();
            
            for(String word : words){
                TrieNode curr = root;
                boolean nodeAdded = false;
    						
                for(int j=0; j<word.length(); j++){
                    if(curr.isWord){
    			//Found prefix in the tree
                        System.out.println("BAD SET");
                        System.out.println(word);
                        return;
                    } 
    								
    		if(!curr.children.containsKey(word.charAt(j))){
                        curr.children.put(word.charAt(j), new TrieNode());
                        nodeAdded = true;
                    }
                    curr = curr.children.get(word.charAt(j));
                }
                curr.isWord = true;
                if(!nodeAdded){
    		//No new node added means the word is a prefix
                    System.out.println("BAD SET");
                    System.out.println(word);
                    return;
                }
            }
            System.out.println("GOOD SET");
    
  • + 0 comments

    The description seems to be unclear. I solved the task in two different ways with the same result: basic (open) tests passes, but most of closed tests fails.

  • + 0 comments

    when you are searching through a tire tree, you should be able to detect whether the current path contains a prefix or the current string you are searching is a prefix of another string.

    // if any prefix existing on this path
    bool checkPrefix(string& word, TrieNode* node){
        TrieNode* current = node;
        for(char ch:word){
            ch -= 'a';
            if(!current->children[ch]) break;
            current = current->children[ch];
            if(current->isEnd) return true;
        }
        return false;
    }
    // if the current string is a prefix of other string
    bool isPrefix(string& word, TrieNode* node){
        TrieNode* current = node;
        for(char ch:word){
            ch -= 'a';
            if(!current->children[ch]) return false;
            current = current->children[ch];
        }
        
        for(auto& next:current->children){
            if(next) return true;
        }
        return false;
    }
    
  • + 0 comments

    Ok, you need to tell me exactly which word I'm supposed to print if it's a bad set. It says the first word I checked where it failed, but there are several ways in which order I could check these words. Is it the first word that has a prefix?