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.
in C# this will technically solve it, but doesn't fit their time constraints:
publicstaticvoidnoPrefix(List<string>words){List<string>prefixes=newList<string>();foreach(stringwordinwords){foreach(stringtinprefixes){// if word has a prefix (contains prefix) || if word is a prefixif(word.StartsWith(t)||t.StartsWith(word)){Console.WriteLine("BAD SET\n"+word);return;}}prefixes.Add(word);}Console.WriteLine("GOOD SET");}
It seems instead they want you to use a trie or some datastructure.
The trie approach works like this:
get word from list
add each character to tree (first round will add each character)
so if you have list [ab], [adc], and [abc]
first word gets added with a node for a, and a node for b from a with b being the end of a word.
adc gets added by starting at a, no branch for d, so new branch. then from d add a node for c and mark c as end of a word as well (both b and c at the end of the tree are marked as end of a word)
for abc, we start at a, see a, see a path for b from a, then from b we see there's an 'end of word' mark so we exit and return 'BAD SET' because this means there's a prefix in abc.
if we had seen abc before ab, when we do ab we would have started with a, gone to b which also already exists. But we would have found we are at the end of this word at a node which has a child. So we also return 'BAD SET' because this means the current word is a subset of a word previously added.
No code 4 you. Go try it.
If you are still lost here's an outline of my solution:
create some sort of Node object for our tree. It needs to store it's child nodes with their character, and whether this node is the end of a word or not.
In our solution - we need to create a root node. It can be empty. (we don't necesarily care about the current node's letter or not, we just care about it's children) This root node does not have a letter for example.
Now we go through our list of words and create or tree:
When inserting a new word we want to start at the root of our tree. For every character in the word, we need to see if our current node has a child node of that character. If it doesnt, we create a new node for that character linked to the current node, and move to it. We continue creating nodes until we get to the end of the word.
If the node has a child node of the current character of the word we are on, we follow it. If at any point we reach a node that is marked as the end of a word, it means the current word contains a prefix of a word previously added to the tree, and is invalid.
If we finish a word, and the last node has any children, the word is a prefix of an existing word, and the tree is invalid.
If not however, it means we have been creating new nodes, so we mark the current node as the end of a word, and continue on to the next word.
If we manage to finish the whole list of words without ever reaching an 'end of word' node before the word is over, or finishing a word on a node that contains more children, we have a list which contains no prefixes, and is valid
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
No Prefix Set
You are viewing a single comment's thread. Return to all comments →
in C# this will technically solve it, but doesn't fit their time constraints:
It seems instead they want you to use a trie or some datastructure.
The trie approach works like this: get word from list add each character to tree (first round will add each character)
so if you have list [ab], [adc], and [abc] first word gets added with a node for a, and a node for b from a with b being the end of a word. adc gets added by starting at a, no branch for d, so new branch. then from d add a node for c and mark c as end of a word as well (both b and c at the end of the tree are marked as end of a word) for abc, we start at a, see a, see a path for b from a, then from b we see there's an 'end of word' mark so we exit and return 'BAD SET' because this means there's a prefix in abc.
if we had seen abc before ab, when we do ab we would have started with a, gone to b which also already exists. But we would have found we are at the end of this word at a node which has a child. So we also return 'BAD SET' because this means the current word is a subset of a word previously added.
No code 4 you. Go try it.
If you are still lost here's an outline of my solution:
create some sort of Node object for our tree. It needs to store it's child nodes with their character, and whether this node is the end of a word or not.
In our solution - we need to create a root node. It can be empty. (we don't necesarily care about the current node's letter or not, we just care about it's children) This root node does not have a letter for example.
Now we go through our list of words and create or tree: When inserting a new word we want to start at the root of our tree. For every character in the word, we need to see if our current node has a child node of that character. If it doesnt, we create a new node for that character linked to the current node, and move to it. We continue creating nodes until we get to the end of the word. If the node has a child node of the current character of the word we are on, we follow it. If at any point we reach a node that is marked as the end of a word, it means the current word contains a prefix of a word previously added to the tree, and is invalid.
If we finish a word, and the last node has any children, the word is a prefix of an existing word, and the tree is invalid. If not however, it means we have been creating new nodes, so we mark the current node as the end of a word, and continue on to the next word.
If we manage to finish the whole list of words without ever reaching an 'end of word' node before the word is over, or finishing a word on a node that contains more children, we have a list which contains no prefixes, and is valid