-
Notifications
You must be signed in to change notification settings - Fork 199
/
Trie.java
executable file
·155 lines (119 loc) · 3.71 KB
/
Trie.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
package Algorithms;
import java.util.ArrayList;
class TrieNode {
// Initialize your data structure here.
boolean isLeaf;
boolean visited = false;
TrieNode[] children;
public TrieNode() {
isLeaf = false;
children = new TrieNode[26];
}
public boolean isLeaf() {
// TODO Auto-generated method stub
return isLeaf;
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
public static void main(String[] strs) {
Trie trie = new Trie();
trie.insert("a");
trie.insert("abe");
trie.insert("ac");
trie.insert("bg");
//System.out.println(trie.firstStartsWith("abe"));
System.out.println(trie.findNext("abc"));
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode cur = root;
for (int i = 0; i < word.length(); i++) {
int idx = word.charAt(i) - 'a';
if (cur.children[idx] == null) {
cur.children[idx] = new TrieNode();
}
cur = cur.children[idx];
}
cur.isLeaf = true;
}
public String findNext(String word) {
int len = word.length();
for (int i = len; i >= 0; i--) {
String preFix = word.substring(0, i);
String nextWord = null;
if (i == len) {
nextWord = firstStartsWith(preFix, '1');
} else {
nextWord = firstStartsWith(preFix, word.charAt(i));
}
if (nextWord != null) {
return nextWord;
}
}
return null;
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode cur = root;
for (int i = 0; i < word.length(); i++) {
int idx = word.charAt(i) - 'a';
if (cur.children[idx] == null) {
return false;
}
cur = cur.children[idx];
}
return cur.isLeaf;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode cur = root;
for (int i = 0; i < prefix.length(); i++) {
int idx = prefix.charAt(i) - 'a';
if (cur.children[idx] == null) {
return false;
}
cur = cur.children[idx];
}
return true;
}
public String firstStartsWith(String prefix, char next) {
TrieNode cur = root;
for (int i = 0; i < prefix.length(); i++) {
int idx = prefix.charAt(i) - 'a';
if (cur.children[idx] == null) {
return null;
}
cur = cur.children[idx];
}
String firstStartWith = prefix;
TrieNode tailNode = cur;
boolean firstChar = true;
while (tailNode == cur || !tailNode.isLeaf()) {
int i = 0;
if (firstChar) {
i = next - 'a' + 1;
}
firstChar = false;
for (; i < 26; i++) {
if (tailNode.children[i] != null) {
char nextChar = (char)('a' + i);
firstStartWith += nextChar;
tailNode = tailNode.children[i];
}
}
// No child
if (i >= 26) {
break;
}
}
if (firstStartWith.equals(prefix)) {
return null;
}
return firstStartWith;
}
}