-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoggleTrieSET.java
More file actions
70 lines (55 loc) · 1.59 KB
/
Copy pathBoggleTrieSET.java
File metadata and controls
70 lines (55 loc) · 1.59 KB
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
import java.util.Arrays;
public class BoggleTrieSET {
private static final int R = 26;
private TrieNode root;
/**
* Returns
* -1 if not contains
* 0 if has prefix
* 1 if has full match
*/
public TrieNode contains(String key) {
return contains(root, key, 0);
}
public TrieNode contains(TrieNode x, String prefix) {
return contains(x, prefix, prefix.length() - 1);
}
private TrieNode contains(TrieNode x, String key, int d) {
char c;
while (x != null) {
if (d == key.length()) {
return x;
}
c = key.charAt(d);
d += 1;
x = x.next[getCharIdx(c)];
}
return null;
}
private int getCharIdx(char x) {
return ((int) x) - 65;
}
public void add(String key) {
root = add(root, key, 0);
}
private TrieNode add(TrieNode x, String key, int d) {
if (x == null) x = new TrieNode();
if (d == key.length()) {
x.isString = true;
} else {
char c = key.charAt(d);
x.next[getCharIdx(c)] = add(x.next[getCharIdx(c)], key, d + 1);
}
return x;
}
public static void main(String[] args) {
In in = new In("inputs/boggle/dictionary-algs4.txt");
String[] dictionary = in.readAllStrings();
BoggleTrieSET set = new BoggleTrieSET();
for (String word : dictionary) {
set.add(word);
}
TrieNode hel = set.contains("HELL");
System.out.println(set.contains(hel, "HELLO"));
}
}