-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.java
262 lines (176 loc) · 5.84 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
public class Trie {
static class Node{
Node children[] = new Node[26];
boolean eow = false;
public Node() {
for(int i=0;i<26;i++){
children[i] = null;
}
}
}
public static Node root = new Node();
public static void insert(String word){ //O(L)
Node curr = root;
for(int level=0; level<word.length();level++){
int index = word.charAt(level) - 'a';
if(curr.children[index] == null){
curr.children[index] = new Node();
}
curr = curr.children[index];
}
curr.eow = true;
}
public static boolean search(String key){ //O(L)
Node curr = root;
for(int level=0; level<key.length();level++){
int index = key.charAt(level) - 'a';
if(curr.children[index] == null){
return false;
}
curr = curr.children[index];
}
return curr.eow = true;
}
public static boolean wordBreak(String key){
if(key.length() == 0){
return true;
}
for(int i=1;i<=key.length();i++){
//substring(beg idx, last idx)
if(search(key.substring(0, i)) &&
wordBreak(key.substring(i))){
return true;
}
}
return false;
}
//Prefix Problem
static class Node1{
Node1[] children = new Node1[26];
boolean eow;
int freq;
public Node1(){
for(int i=0;i<children.length;i++){
children[i] = null;
}
freq =1;
}
}
public static Node1 root1 = new Node1();
public static void insert1(String word){
Node1 curr1 = root1;
for(int i=0;i<word.length();i++){
int idx = word.charAt(i)-'a';
if(curr1.children[idx] == null){
curr1.children[idx] = new Node1();
} else{
curr1.children[idx].freq++;
}
curr1 = curr1.children[idx];
}
curr1.eow = true;
}
public static void findPrefix(Node1 root1, String ans){ //O(L) longest word
if(root1 == null){
return;
}
if(root1.freq == 1){
System.out.println(ans);
return;
}
for(int i=0;i<root1.children.length;i++){
if(root1.children[i] != null){
findPrefix(root1.children[i], ans+(char)(i+'a'));
}
}
}
// startsWith problem
public static boolean startsWith(String prefix){
Node curr = root;
for(int i=0;i<prefix.length();i++){
int idx = prefix.charAt(i) - 'a';
if(curr.children[idx] == null){
return false;
}
curr = curr.children[idx];
}
return true;
}
// Count Unique Substrings
// count Nodes
public static int countNodes(Node root){
if(root == null){
return 0;
}
int count = 0;
for(int i=0;i<26;i++){
if(root.children[i] != null){
count += countNodes(root.children[i]);
}
}
return count+1;
}
// Longest Word with all Prefixes
public static String ans = "";
public static void longetsWord(Node root, StringBuilder temp){
if(root == null){
return;
}
for(int i=0;i<26;i++){
if(root.children[i] != null && root.children[i].eow == true){
char ch = (char)(i+'a');
temp.append(ch);
if(temp.length() > ans.length()){
ans = temp.toString();
}
longetsWord(root.children[i], temp);
temp.deleteCharAt(temp.length()-1);//backtrack
}
}
}
public static void main(String[] args) {
// String words[] = {"the", "a", "there", "their", "any", "thee"};
// for(int i=0;i<words.length;i++){
// insert(words[i]);
// }
// System.out.println(search("thee"));
// System.out.println(search("thor"));
// String arr[] = {"i", "like", "sam", "samsung", "mobile", "ice"};
// for(int i=0;i<arr.length;i++){
// insert(arr[i]);
// }
// String key = "ilikesamsung";
// System.out.println(wordBreak(key));
// Prefix Problem
// String arr[] = {"zebra", "dog", "duck","dove"};
// for(int i=0;i<arr.length;i++){
// insert1(arr[i]);
// }
// root1.freq = -1;
// findPrefix(root1, "");
// startsWith problem
// String words[] = {"apple", "app", "mango", "man", "woman"};
// String prefix1= "app";
// String prefix2= "moon";
// for(int i=0;i<words.length;i++){
// insert(words[i]);
// }
// System.out.println(startsWith(prefix1));
// System.out.println(startsWith(prefix2));
// Count Unique SubStrings
// String str = "ababa";
// //suffix -> insert in trie
// for(int i=0;i<str.length();i++){
// String suffix = str.substring(i);
// insert(suffix);
// }
// System.out.println(countNodes(root));
// Longest Word with all Prefixes
String words[] = {"a", "banana", "app", "appl", "ap", "apply", "apple"};
for(int i=0;i<words.length;i++){
insert(words[i]);
}
longetsWord(root, new StringBuilder(""));
System.out.println(ans);
}
}