You are viewing a single comment's thread. Return to all comments →
Passing All the test cases
static class Node{ Node links[]; boolean isEnd; public Node() { links = new Node[10]; isEnd = false; } } private static Node root = new Node(); public static boolean insert(String str) { Node node = root; int n = str.length(); for(int i=0;i<n;i++) { int val = str.charAt(i)-'a'; if(node.links[val]==null) { node.links[val] = new Node(); node = node.links[val]; } else { node = node.links[val]; if(node.isEnd || i==n-1) return true; } } node.isEnd = true; return false; } public static void noPrefix(List<String> words) { root = new Node(); int n = words.size(), badIndex = n; boolean isBad = false; for(int i=0;i<n;i++) { if(insert(words.get(i))) { badIndex = i; isBad=true; break; } } if(isBad) { System.out.println("BAD SET"); System.out.println(words.get(badIndex)); } else { System.out.println("GOOD SET"); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
No Prefix Set
You are viewing a single comment's thread. Return to all comments →
Passing All the test cases