• + 0 comments

    Java 8 solution, passed all the tests

        public static void noPrefix(List<String> words) {
            final Set<String> strings = new HashSet<>();
            final Set<String> subSets = new HashSet<>();
            final int size = words.size();
            for (int i=0; i< size; i++){
                final String checkedWord = words.get(i);
                //Check if the current word is subset of previous words
                if(subSets.contains(checkedWord)){
                    System.out.println("BAD SET");
                    System.out.print(checkedWord);
                    return;
                }
                
                for(int j=1; j<=checkedWord.length(); j++){
                    final String subSet = checkedWord.substring(0, j);
                    //Check if any previous word is a subset of the current word
                    if(strings.contains(subSet)){
                        System.out.println("BAD SET");
                        System.out.print(checkedWord);
                        return;
                    }
                    subSets.add(subSet);
                }
                strings.add(checkedWord);
            }
            
            System.out.println("GOOD SET");
    
        }