No Prefix Set

Sort by

recency

|

228 Discussions

|

  • + 0 comments

    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.

    def noPrefix(words):
        if len(words) == 1:
            return
        
        seen = set()
    
        for i in range(len(words)):
            target = words[i]
            seen.add(target)
            for j in range(i, len(words)):
                if i == j:
                    continue
    
                if words[j] in seen:
                    print("BAD SET")
                    print(words[j])
                    return
                    
                if len(words[j]) > len(target) and words[j][0:len(target)] in seen:
                    print("BAD SET")
                    print(words[j])
                    return
    
  • + 0 comments

    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:

        public static void noPrefix(List<string> words)
        {
            for(int word = 0; word < words.Count; word++){
                for(int prefix = 0; prefix < words.Count; prefix ++){
                    if(word == prefix) continue;
                    if(words[word].StartsWith(words[prefix])){
                        Console.WriteLine($"BAD SET");
                        Console.WriteLine(words[word]);
                        return;
                    }
                }
            }
            Console.WriteLine("GOOD SET");
        }
    
  • + 0 comments

    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.

  • + 2 comments

    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?

    • + 0 comments

      Their answer is bad and is tailored to one specific implementation.

    • + 0 comments

      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.

  • + 1 comment

    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 !

    def noPrefix(words):
        #Write your code here
        _end = "_end"
        trie = {}
        
        #return the current word where the check fails
        def bad(s):
            print("BAD SET")
            print(s)
        
        #incremental insertion of words
        for word in words:
            current = trie
            #for each word, insert and change current map/dict per letter
            for c in word:
                #FAIL: a processed word is the prefix of the current word
                if _end in current:
                    bad(word)
                    return
                #create map/dict if missing
                if c not in current:
                    current[c] = {}
                current = current[c]
            
            #FAIL: current word map/dict is not empty
            #meaning either:
            #- current word was already inserted in the trie
            #- current word was a prefix of previously inserted words
            if len(current) > 0:
                bad(word)
                return
            
            #mark the current node where the current word ends here
            current[_end] = _end
        print("GOOD SET")
    
    • + 1 comment

      Java 8 version (not type-safe(?)) simplified and updated thanks to @dgzwiro !

      public static final char END = '_';
      public static final String BAD_SET_FORMAT = "BAD SET\n%s";
      
      public static void noPrefix(List<String> words) {
      // Write your code here
          Map<Character, Object> trie = new HashMap<>();
          Map<Character, Object> current = null;
          // incremental insertion of words
          for (String word: words) {
              current = trie;
              // for each word, insert and change current map/dict per letter
              for (char c: word.toCharArray()) {
                  // FAIL: a processed word is the prefix of the current word
                  if (current.containsKey(END)) {
                      System.out.printf(BAD_SET_FORMAT, word);
                      return;
                  }
                  // create map/dict if missing
                  if (!current.containsKey(c)) {
                      current.put(c, new HashMap<Character, Object>());
                  }
                  current = (HashMap<Character, Object>) current.get(c);
              }
              
              // FAIL: current word map/dict is not empty
              // meaning either:
              // - current word was already inserted in the trie
              // - current word was a prefix of previously inserted words
              if (!current.isEmpty()) {
                  System.out.printf(BAD_SET_FORMAT, word);
                  return;
              }
              
              // mark the current node where the current word ends here
              current.put(END, null);
          }
          System.out.println("GOOD SET");
      }
      
      • + 1 comment

        I guess that's stupid question, but why are you using

            if (current.keySet().stream().filter(x -> !x.equals(END)).findAny().isPresent()) {
                System.out.printf(BAD_SET_FORMAT, word);
                return;
            }
        
            if (current.containsKey(END)) {
                System.out.printf(BAD_SET_FORMAT, word
                return;
            }
        

        instead of

            if (!current.isEmpty()){
                System.out.printf(BAD_SET_FORMAT, word
                return;
            }
        
        • + 0 comments

          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!