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
|
225 Discussions
|
Please Login in order to post a comment
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?
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):
I'd love to hear some opinions on my solution.
def noPrefix(words): word_dict = dict()
Tried using
.sort()
such that the solution boils down to comparing neighbouring words. Unfortunately this violates the "tested first" condition due to the reordering of words:The test cases are bad. They are bad because author is making an assumption on which data structure the developer is going to use. If you try to brute force the problem then you might be left scratching your head as to why it is not working. But if you use a Trie then the test cases make sense.
For example on test case 1:
If you use brute force you will check first if aab is a prefix of any other words in the list. This means that you will test aabghgh first and that is what you will print.
If you use a Trie tree, then you will first start building the tree with aab, next you will modify the tree with aac, then you will begin to modify the tree with aacghgh. At this point you will find that aacghgh does have a prefix, and it will get printed, thus passing the test case.
Test cases should be written in a way that are agnostic of the algorithm that the developer uses to solve it. There are always so many ways to solve the same problem. If the test cases are not agnostic then it should be clear to the developer what algorithm should be used up front.