We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- No Prefix Set
- Discussions
No Prefix Set
No Prefix Set
Sort by
recency
|
40 Discussions
|
Please Login in order to post a comment
C# solution using a Trie
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
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; } }
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".
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):
Hope this helps!