No Prefix Set

Sort by

recency

|

40 Discussions

|

  • + 0 comments

    C# solution using a Trie

    class TrieNode
    {
        public TrieNode[] Child = new TrieNode[10];
        public bool WordEnd = false;
    }
    
    class Trie
    {
        public TrieNode Root = new TrieNode();
        
        // Inserts s into the Trie and also checks if s is a prefix or has a prefix in the Trie.
        public bool Insert(string s)
        {
            bool hasPrefix = false;
            bool isPrefix = true;
            TrieNode currentNode = this.Root;
            
            foreach(char ch in s)
            {
                if (currentNode.WordEnd)
                {
                    hasPrefix = true;
                }
                
                int index = ch - 'a';
                
                if (currentNode.Child[index] == null)
                {
                    currentNode.Child[index] = new();
                    isPrefix = false;
                }
                
                currentNode = currentNode.Child[index];
            }
            
            currentNode.WordEnd = true;
            
            return hasPrefix || isPrefix;
        }
    }
    
    class Result
    {
        public static void noPrefix(List<string> words)
        {
            Trie trie = new();
            
            foreach (string word in words)
            {
                bool prefix = trie.Insert(word);
                
                if (prefix)
                {
                    Console.WriteLine("BAD SET");
                    Console.WriteLine(word);
                    return;
                }
            }
            
            Console.WriteLine("GOOD 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
    

  • + 0 comments

    One more c++ on Prefix Tree.

    class OrderSet{ struct Tries{ unordered_map children; bool endOfWorld;

    Tries(): endOfWorld(false){}; };

    public:
    Tries * root; OrderSet(): root (new Tries()){};

    void insert(string str){ Tries * current = root; for(auto c: str){ if(current->children.find(c) == current->children.end()){ current->children[c]= new Tries(); } current = current->children[c]; } current->endOfWorld=true;
    }

    bool insertCheck(string str){ Tries * current = root; for(int i=0; i < str.size(); i++){ if(current->children.find(str[i]) == current->children.end()){ current->children[str[i]]= new Tries(); } else{ if(i+1 == str.size()) return false; } if(current->endOfWorld){ return false; } current = current->children[str[i]]; } current->endOfWorld=true; return true;
    }

    };

    void noPrefix(vector words) {

    OrderSet tree; tree.insert(words[0]); int i; bool bad_set=false; for(i=1; i < words.size(); i++){ if(!tree.insertCheck(words[i])){ bad_set = true; break; }
    }

    if(bad_set){ cout << "BAD SET" << endl; cout << words[i] << endl; } else { cout << "GOOD SET" << endl; } }

  • + 0 comments

    OMG the description is so confusing and wrong!!! Totally wrong about what they want to see. HackerRank has so much ambiguity and yet they are a bottleneck of code screening for many companies. This task is about building a Trie (Prefix tree) and looking for existing words. Nothing about word prefixes. Example 'as', 'sfdhk', 's' Will result in BAD SET "s", not "sfdhk".

  • + 0 comments

    If you are also wondering why the correctness is only 50%, please remember the following test cases (though I really think the problem should include more examples for the ambiguous results):

    1. ['gummy', 'gum'] => returns 'gum'
    2. ['gum', 'gummy'] => returns 'gummy'

    Hope this helps!