-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo Prefix Set.java
61 lines (43 loc) · 1.58 KB
/
No Prefix Set.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Problem Title: No Prefix Set
Problem URL: https://www.hackerrank.com/challenges/one-month-preparation-kit-no-prefix-set/problem?h_l=interview&playlist_slugs%5B%5D=preparation-kits&playlist_slugs%5B%5D=one-month-preparation-kit&playlist_slugs%5B%5D=one-month-week-four
Max Score: 100
Score: 100
Language: Java
Category: One Month Preparation
*/
class Result {
static class SpecialNode {
Map<Character, SpecialNode> map = new HashMap<>();
boolean isComplete;
public boolean add(String word) {
return add(word, 0);
}
private boolean add(String word, int index) {
if(isComplete)
return false;
if(index == word.length()) {
isComplete = true;
return true;
}
SpecialNode child = map.get(word.charAt(index));
if(child == null) {
child = new SpecialNode();
map.put(word.charAt(index), child);
}
else if(index+1 == word.length())
return false;
return child.add(word, index+1);
}
}
public static void noPrefix(List<String> words) {
SpecialNode head = new SpecialNode();
for (int i = 0; i < words.size(); ++i)
if (!head.add(words.get(i)))
{
System.out.printf("BAD SET\n%s\n", words.get(i));
return;
}
System.out.println("GOOD SET");
}
}