-
Notifications
You must be signed in to change notification settings - Fork 0
/
HackerrankTries.java
74 lines (60 loc) · 1.45 KB
/
HackerrankTries.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
import java.util.*;
public class HackerrankTries {
public static class TrieNode {
TrieNode[] children;
char val;
int count;
// default root node
public TrieNode() {
children = new TrieNode[26];
count = 0;
}
// intermediate nodes
public TrieNode(char c) {
this();
val = c;
count = 1;
}
// Add new contact string
public void Add(String contact) {
int pos = contact.charAt(0) - 'a';
if (children[pos] == null) {
children[pos] = new TrieNode(contact.charAt(0));
} else {
children[pos].count++;
}
if (contact.length() > 1) {
children[pos].Add(contact.substring(1));
}
}
// Query for existing contact string
public int Find(String contact) {
int pos = contact.charAt(0) - 'a';
if (children[pos] == null) {
return 0;
} else if (contact.length() == 1) {
return children[pos].count;
} else {
return children[pos].Find(contact.substring(1));
}
}
}
public static void main(String[] args) {
TrieNode ContactTrie = new TrieNode();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int a0 = 0; a0 < n; a0++) {
String op = in.next();
String contact = in.next();
// System.out.println(op);
if (op.equals("add")) {
// System.out.println("reached add");
ContactTrie.Add(contact);
} else if (op.equals("find")) {
// System.out.println("reached find");
System.out.println(ContactTrie.Find(contact));
}
}
in.close();
}
}