-
Notifications
You must be signed in to change notification settings - Fork 0
/
BabyNames.java
245 lines (221 loc) · 6.29 KB
/
BabyNames.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import java.util.HashMap;
public class BabyNames {
public static void main(String[] args) {
Kattio sc = new Kattio(System.in, System.out);
int i = sc.getInt();
Trie tree = new Trie();
while (i != 0) {
if (i == 1) {
String s = sc.getWord();
int strLen = s.length();
if (strLen > 0)
tree.insert(s, strLen, sc.getInt());
} else if (i == 3) {
sc.println(tree.query(sc.getWord(), sc.getWord(), sc.getInt()));
} else {
tree.remove(sc.getWord());
}
i = sc.getInt();
}
sc.close();
}
}
class Trie {
TrieNode root = new TrieNode(new Array(), null, null);
//o(26 + 29) ops max
public int query(String start, String end, int val) {
TrieNode curr = this.root;
int i = (int) start.charAt(0) - 65;
int j = (int) end.charAt(0) - 65;
int result1 = 0;
int result2 = 0;
for (int k = i; k < j; k++) {
if (curr.arr.get(k) != null) {
result1 += curr.arr.get(k).n1;
result2 += curr.arr.get(k).n2;
}
}
int endLen = end.length();
curr = curr.arr.get((int) end.charAt(0) - 65);
if (endLen > 1 && curr != null) {
int m = 1;
while (m < endLen) {
int indx = (int) end.charAt(m) - 65;
int minusFactor1 = 0;
int minusFactor2 = 0;
if (curr.arr.get(indx) != null) {
minusFactor1 = curr.arr.get(indx).n1;
minusFactor2 = curr.arr.get(indx).n2;
}
result1 += curr.n1 - minusFactor1;
result2 += curr.n2 - minusFactor2;
TrieNode t = curr.arr.get(indx);
if (t == null) {
break;
} else {
curr = t;
}
m++; //indx of end str not in tree
}
}
if (val == 1) {
return result1;
} else if (val == 2) {
return result2;
} else {
return result1 + result2;
}
}
// O(2L) + 26
public void insert(String s, int strLen, int v) {
char c = s.charAt(0);
int indx = (int) c - 65;
Array arr = this.root.arr;
this.root.updateP1(indx);
TrieNode n = insert(s, 0, v, arr.get(indx), this.root, strLen);
arr.add(indx, n);
this.root.updateP2(indx);
}
// O(2L) -> L is length of string
public TrieNode insert(String s, int i, int v, TrieNode curr, TrieNode parent, int strLen) {
if (i == strLen - 1) {
if (curr == null) {
TrieNode t = new TrieNode(new Array(), new Value(v), parent);
t.updateNode(v);
return t;
} else { // curr != null
if (curr.v != null) {
curr.removeV(curr.v.value);
}
curr.updateNode(v);
curr.v = new Value(v);
return curr;
}
} else {
char c = s.charAt(i + 1);
int indx = (int) c - 65;
TrieNode b = curr;
if (curr == null) {
b = new TrieNode(new Array(), null, parent);
}
b.updateP1(indx);
TrieNode n = insert(s, i + 1, v, b.arr.get(indx), curr, strLen);
b.arr.add(indx, n);
b.updateP2(indx);
return b;
}
}
// O(2L)
public void remove(String s) {
int indx = (int) s.charAt(0) - 65;
this.root.updateP1(indx);
remove(s, 0, this.root, s.length());
this.root.updateP2(indx);
}
// O(2L) -> L is length of string
public TrieNode remove(String s, int i, TrieNode curr, int strLen) {
if (curr == null) {
return null;
} else if (strLen == 1) {
int indx = (int) s.charAt(i) - 65;
TrieNode t = curr.arr.get(indx);
if (t.v == null) {
return null;
} else if (t.arr.length > 0) {
t.removeV(t.v.value);
t.v = null;
return null;
} else {
curr.updateP1(indx);
curr.arr.remove(indx);
return curr;
}
} else {
int indx = (int) s.charAt(i) - 65;
curr.updateP1(indx);
TrieNode removalNode = remove(s, i + 1, curr.arr.get(indx), strLen - 1);
if (removalNode != null && curr.arr.length == 1) {
curr.arr.remove(indx);
curr.updateP2(indx);
return curr;
} else {
curr.updateP2(indx);
return null;
}
}
}
@Override
public String toString() {
return this.root.toString();
}
}
class TrieNode {
Array arr;
Value v;
TrieNode parent;
int n1 = 0;
int n2 = 0;
public TrieNode(Array arr, Value k, TrieNode parent) {
this.arr = arr;
this.v = k;
this.parent = parent;
}
public void updateNode(int v) {
if (v == 1) {
n1++;
} else {
n2++;
}
}
public void removeV(int v) {
if (v == 1) {
n1--;
} else {
n2--;
}
}
public void updateP1(int indx) {
TrieNode t = this.arr.get(indx);
if (t != null) {
n1 -= t.n1;
n2 -= t.n2;
}
}
// O(26)
public void updateP2(int indx) {
TrieNode t = this.arr.get(indx);
if (t != null) {
n1 += t.n1;
n2 += t.n2;
}
}
@Override
public String toString() {
return arr.toString();
}
}
class Value {
int value;
public Value(int v) {
this.value = v;
}
}
class Array {
int length = 0;
TrieNode[] map = new TrieNode[26];
void add(int i, TrieNode t) {
map[i] = t;
length++;
}
void remove(int i) {
map[i] = null;
length--;
}
TrieNode get(int i) {
return map[i];
}
@Override
public String toString() {
return map.toString();
}
}