-
Notifications
You must be signed in to change notification settings - Fork 0
/
P75.java
45 lines (40 loc) · 1 KB
/
P75.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
class tn {
tn[] c = new tn[26];
boolean end = false;
}
public class P75 {
static tn root;
P75() {
root = new tn();
}
public static void insert(String w) {
tn node = root;
for (int i = 0; i < w.length(); i++) {
int p = w.charAt(i) - 'a';
if (node.c[p] == null)
node.c[p] = new tn();
node = node.c[p];
}
node.end = true;
}
public static boolean search(String w) {
tn node = root;
for (int i = 0; i < w.length(); i++) {
int p = w.charAt(i) - 'a';
if (node.c[p] == null)
return false;
node = node.c[p];
}
return node.end;
}
public static boolean startsWith(String w) {
tn node = root;
for (int i = 0; i < w.length(); i++) {
int p = w.charAt(i) - 'a';
if (node.c[p] == null)
return false;
node = node.c[p];
}
return true;
}
}