-
Notifications
You must be signed in to change notification settings - Fork 11
/
Contacts.java
181 lines (149 loc) Β· 4.33 KB
/
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package trie;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Letter {
private HashMap<Character, Letter> map;
private boolean isWord;
private char letter;
private int words = 0;
Letter(char letter){
map = new HashMap<>();
isWord = false;
this.letter = letter;
}
Letter(boolean isWord, char letter){
this.isWord = isWord;
map = new HashMap<>();
this.letter = letter;
}
public boolean isWord(){
return isWord;
}
public char value(){
return letter;
}
public int getWords(){
return words;
}
public void addWord(String word){
words++;
if(word.length() == 0){
this.isWord = true;
return;
}
if(map.containsKey(word.charAt(0)))
map.get(word.charAt(0)).addWord(word.substring(1));
else {
map.put(word.charAt(0), new Letter(word.charAt(0)));
map.get(word.charAt(0)).addWord(word.substring(1));
}
}
public void print(String name){
name = name + letter;
if(isWord)
System.out.println(name);
for(Map.Entry<Character, Letter> letterEntry : map.entrySet()){
letterEntry.getValue().print(name);
}
}
public int numberOfClosestEntries(String word){
if(word.length() == 0)
return words;
if(map.containsKey(word.charAt(0)))
return map.get(word.charAt(0)).numberOfClosestEntries(word.substring(1));
return 0;
}
public boolean containsPrefix(String word){
if(word.length() == 0 || words <= 1)
return false;
if(isWord)
return true;
if(map.containsKey(word.charAt(0)))
return map.get(word.charAt(0)).containsPrefix(word.substring(1));
return false;
}
}
class Node{
private HashMap<Character, Letter> map;
Node(){
map = new HashMap<>();
}
public boolean containsLetter(char letter){
return map.containsKey(letter);
}
public void add(char letter){
map.put(letter, new Letter(letter));
}
public void addWord(String word){
map.get(word.charAt(0)).addWord(word.substring(1));
}
public void print(){
for(Map.Entry<Character, Letter> letterEntry : map.entrySet()){
letterEntry.getValue().print("");
}
}
public int numberOfClosestEntries(String word){
if(word.length() == 0)
return map.size();
if(map.containsKey(word.charAt(0)))
return map.get(word.charAt(0)).numberOfClosestEntries(word.substring(1));
return 0;
}
public boolean containsPrefix(String word){
if(word.length() == 0)
return false;
if(map.containsKey(word.charAt(0))){
Letter firstLetter = map.get(word.charAt(0));
if(firstLetter.getWords() > 1)
return firstLetter.isWord() || firstLetter.containsPrefix(word.substring(1));
return false;
}
return false;
}
}
class Trie {
private Node start;
Trie(){
start = new Node();
}
public void addWord(String string){
if(string.length() == 0)
return;
if(start.containsLetter(string.charAt(0))){
start.addWord(string);
} else {
start.add(string.charAt(0));
start.addWord(string);
}
}
public void print(){
start.print();
}
public int numberOfClosestEntries(String word){
return start.numberOfClosestEntries(word);
}
public boolean containsPrefix(String word){
return start.containsPrefix(word);
}
}
public class Contacts {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int queries = in.nextInt();
performQueries(queries);
}
private static void performQueries(int queries){
Trie contacts = new Trie();
while (queries-- > 0){
String operation = in.next();
String name = in.next();
if(operation.charAt(0) == 'a'){
contacts.addWord(name);
} else {
System.out.println(contacts.numberOfClosestEntries(name));
}
}
contacts.print();
}
}