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
|
228 Discussions
|
Please Login in order to post a comment
From reading this discussion it seems this is seeing if we used a trie. Even so, this doesn't timeout and I think the logic seems to check out. Given their criteria that words are tested in order, and the prefix 'aab' is lower indexed compared to 'aac'. Guess im skipping this one.
after looking at the discussions i get that the question is looking for a Trie implementation. but i just can't see how the following solution doesn't work for all the test cases, because as far as i can see it satisfies every requirement in the question:
I have an issue with the problem description, as it is tailored to a specific solution. Not only having to print "the string being checked" instead of specifically the prefix or string that contains the prefix is inconsistent, but correct answers are marked as wrong. For example, in the test case they show:
4 aab aac aacghgh aabghgh
there are actually FOUR correct answers: aab is a prefix of aabghgh and aac is a prefix of aacghgh. Since we can print any string of the prefix-prefixed pair, we could technically print any of these and be correct. HOWEVER, only BAD SET aacghgh is correct, because they assume we will be solving the problem in a specific way.
Good problem but bad solution criteria, in my opinion.
I'm in doubt here. Following the problem description and samples explanations, actual test case 1 should print BAD SET with the third string "edchgb", but the test case expect an output of "d" which is evaluated later. Also, "d" is the prefix, not the string that contains it. Can someone explain me this?
Their answer is bad and is tailored to one specific implementation.
Same thing happened to me. My solution was only checking if the current word contained the ending of a previous word in the list (meaning the current word has a previous word as a prefix).
The solution should also check the reverse (i.e., is the current word the prefix of a previous word?). I did this by using a Trie (which you probably already did) and, after inserting the full word into the Trie (i.e., after reaching the end of the word), checking if the ending node has any children. If yes, the current word is the prefix of a previous word.
my Python 3 attempt by incrementally inserting words into a trie (thanks to inspiration from many of you guys) (must be some other "cleaner" way to do the create dict if not exists step, but I prefer readability in my own terms XD):
edit: simplified and updated thanks to @dgzwiro !
Java 8 version (not type-safe(?)) simplified and updated thanks to @dgzwiro !
I guess that's stupid question, but why are you using
instead of
hello, there are no stupid questions! I guess you are correct, as the for loop has already inserted every single char of the currently processing word, if this path points to even anything (no matter a longer word of the same prefix or the same word) it is already a bad set indicating the current word is a prefix of any previous word. your suggestion is definitely better!