-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP42577.java
More file actions
50 lines (43 loc) · 1.6 KB
/
P42577.java
File metadata and controls
50 lines (43 loc) · 1.6 KB
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
package programmers;
public class P42577 {
class Trie {
Trie[] children;
int numTotalChildren;
public Trie() {
this.children = new Trie[10];
}
public void insert(String phoneNumber) {
Trie node = this;
for (int i = 0; i < phoneNumber.length(); i++) {
int digit = phoneNumber.charAt(i) - '0';
if (node.children[digit] == null)
node.children[digit] = new Trie();
node = node.children[digit];
node.numTotalChildren++;
}
}
public Trie startsWith(String phoneNumber) {
Trie node = this;
for (int i = 0; i < phoneNumber.length(); i++)
node = node.children[phoneNumber.charAt(i) - '0'];
return node;
}
public int getTotalChildren(String phoneNumber) {
return this.startsWith(phoneNumber).numTotalChildren;
}
}
public boolean solution(String[] phoneNumbers) {
Trie trie = new Trie();
for (String num : phoneNumbers)
trie.insert(num);
for (String num : phoneNumbers)
if (trie.getTotalChildren(num) > 1)
return false;
return true;
}
public static void main(String[] args) {
System.out.println(new P42577().solution(new String[] { "119", "97674223", "1195524421" }));
System.out.println(new P42577().solution(new String[] { "123", "456", "789" }));
System.out.println(new P42577().solution(new String[] { "12", "123", "1235", "567", "88" }));
}
}