-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSolution.java
77 lines (61 loc) · 2.27 KB
/
Solution.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.*;
import java.io.*;
import java.math.*;
class Solution {
static class _Node<K> extends HashMap<K, _Node<K>> {
private static final long serialVersionUID = 1L;
private K content;
private HashMap<K, _Node<K>> children = new HashMap<>();
public _Node(K content) {
super();
this.content = content;
}
public void addChild(K content) {
this.children.put(content, new _Node<>(content));
}
public boolean hasChild(K content) {
return this.children.containsKey(content);
}
public _Node<K> getChild(K content) {
return this.children.get(content);
}
public int count() {
int c = 1;
for (_Node<K> aChild : this.children.values())
c += aChild.count();
return c;
}
public String toString() {
String str = this.content + "";
for (_Node<K> aChild : this.children.values())
str += aChild.toString();
return str;
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
HashMap<Integer, _Node<Integer>> trees = new HashMap<>();
for (int i = 0; i < n; i++) {
String telephone = in.next();
// Init tree if it does not exists yet
Integer firstDigit = Integer.parseInt(telephone.charAt(0) + "");
if (!trees.containsKey(firstDigit))
trees.put(firstDigit, new _Node<>(firstDigit));
// Insert digits in tree
_Node<Integer> t = trees.get(firstDigit);
for (int y = 1; y < telephone.length(); y++) {
// Init node if it does not exists yet
Integer currentDigit = Integer.parseInt(telephone.charAt(y) + "");
if (!t.hasChild(currentDigit))
t.addChild(currentDigit);
t = t.getChild(currentDigit);
}
}
int count = 0;
for (Solution._Node<Integer> aTree : trees.values())
count += aTree.count();
// The number of elements (referencing a number) stored in the structure.
System.out.println(count);
}
}