No Prefix Set

  • + 0 comments
    public class TrieNode {
            char value;
            boolean isLast;
            Map<Character, TrieNode> next = new HashMap<>();
            
            public TrieNode(char value, boolean isLast) {
                this.value = value;
                this.isLast = isLast;
            }
        }
        
        public static void noPrefix(List<String> words) {
            TrieNode root = new Result().new TrieNode('z', false);
            for (String word: words) {
                TrieNode node = root;
                for (int i = 0; i < word.length(); i++) {
                    char c = word.charAt(i);
                    TrieNode temp = new Result().new TrieNode(c, i == word.length() -1 ? true : false);
                    
                    if(!node.next.containsKey(c)) {
                        node.next.put(c, temp);
                    } else {
                        temp = node.next.get(c);
                        temp.isLast = temp.isLast || i == (word.length() - 1);
                        
                        if(temp.isLast) {
                            System.out.println("BAD SET");
                            System.out.println(word);
                            return;
                        }
                    } 
                    node = temp;
                }
            }
            System.out.println("GOOD SET");
        }