-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathTries:Contacts.java
61 lines (50 loc) · 1.61 KB
/
Tries:Contacts.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
import java.io.*;
import java.util.*;
public class Contacts {
static class Trie {
int count;
Trie[] children;
public Trie() {
children = new Trie[26];
}
void addWord(String word) {
// System.out.println("Adding " + word);
count++;
if(word.isEmpty()) return;
char first = word.charAt(0);
if(children[first-'a'] == null){
children[first-'a'] = new Trie();
}
children[first-'a'].addWord(word.substring(1));
}
int findWord(String word) {
// System.out.println("Finding " + word);
if(word.isEmpty()) return count;
char first = word.charAt(0);
if(children[first-'a'] == null) return 0;
return children[first-'a'].findWord(word.substring(1));
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
Trie trie = new Trie();
StringBuilder res = new StringBuilder();
String[] opContact = new String[2];
String op, contact;
int count;
for (int nItr = 0; nItr < n; nItr++) {
opContact = sc.nextLine().split(" ");
op = opContact[0];
contact = opContact[1];
if (op.equals("add")) {
trie.addWord(contact);
} else {
count = trie.findWord(contact);
res.append(count + "\n");
}
}
System.out.println(res.toString());
sc.close();
}
}