-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchSuggestionsSystem.java
More file actions
337 lines (262 loc) · 9.89 KB
/
SearchSuggestionsSystem.java
File metadata and controls
337 lines (262 loc) · 9.89 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package Algorithms.Tries;
import java.util.*;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 19 May 2025
*/
public class SearchSuggestionsSystem {
public static void main(String[] args) {
String[] products = {"mobile","mouse","moneypot","monitor","mousepad"};
String searchWord = "mouse";
System.out.println("suggestedProducts => " + suggestedProducts(products, searchWord));
System.out.println("suggestedProductsUsingTwoPointers => " + suggestedProductsUsingTwoPointers(products, searchWord));
System.out.println("suggestedProductsMyApproach => " + suggestedProductsMyApproach(products, searchWord));
System.out.println("suggestedProductsUsingTrie => " + suggestedProductsUsingTrie(products, searchWord));
System.out.println("suggestedProductsUsingTrieAndSuggestionsArray => " + suggestedProductsUsingTrieAndSuggestionsArray(products, searchWord));
}
public static List<List<String>> suggestedProducts(String[] products, String searchWord) {
List<List<String>> ans = new ArrayList<>();
if (products == null || products.length == 0 || searchWord == null || searchWord.length() == 0) return ans;
Arrays.sort(products);
for (int i = 0; i < searchWord.length(); i++) {
List<String> curr = new ArrayList<>();
for (int j = 0; j < products.length; j++) {
if (products[j].startsWith(searchWord.substring(0, i + 1))) curr.add(products[j]);
if (curr.size() == 3) break;
}
ans.add(curr);
}
return ans;
}
/**
* @TimeComplexity nlogn + n*w + m, where w is length of each word
*/
public static List<List<String>> suggestedProductsUsingTwoPointers(String[] products, String searchWord) {
List<List<String>> res = new ArrayList<>();
Arrays.sort(products);
int n = products.length, l = 0, r = n - 1;
for (int i = 0; i < searchWord.length(); i++) {
char c = searchWord.charAt(i);
while (l <= r && (products[l].length() <= i || products[l].charAt(i) < c)) l++;
while (l <= r && (products[r].length() <= i || products[r].charAt(i) > c)) r--;
// if (l > r) {
// res.add(new ArrayList<>());
// continue;
// }
List<String> curr = new ArrayList<>();
for (int j = l; j <= r && curr.size() < 3; j++) {
curr.add(products[j]);
}
res.add(curr);
}
return res;
}
public static List<List<String>> suggestedProductsUsingTrie(String[] products, String searchWord) {
List<List<String>> res = new ArrayList<>();
if (products == null || products.length == 0 || searchWord == null || searchWord.length() == 0) return res;
Trie trie = new Trie();
for (String product : products) trie.insert(product);
for (int i = 0; i < searchWord.length(); i++) {
List<String> curr = new ArrayList<>();
List<String> suggestions = trie.search(searchWord.substring(0, i + 1));
Collections.sort(suggestions);
for (String suggestion : suggestions) {
if (curr.size() < 3 && suggestion.startsWith(searchWord.substring(0, i + 1))) curr.add(suggestion);
else if (curr.size() >= 3) break;
}
res.add(curr);
}
return res;
}
static class Trie {
class TrieNode {
TrieNode[] children;
boolean isEnd;
public TrieNode() {
children = new TrieNode[26];
isEnd = false;
}
}
TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode curr = root;
for (char c : word.toCharArray()) {
if (curr.children[c - 'a'] == null) {
curr.children[c - 'a'] = new TrieNode();
}
curr = curr.children[c - 'a'];
}
curr.isEnd = true;
}
public List<String> search(String word) {
List<String> res = new ArrayList<>();
TrieNode curr = root;
for (char c : word.toCharArray()) {
if (curr.children[c - 'a'] == null) return res;
curr = curr.children[c - 'a'];
}
dfs(curr, word, res);
return res;
}
private void dfs(TrieNode node, String prefix, List<String> res) {
if (node.isEnd) res.add(prefix);
for (int i = 0; i < 26; i++) {
if (node.children[i] != null) {
dfs(node.children[i], prefix + (char)('a' + i), res);
}
}
}
}
static class TrieNode{
TrieNode[] children = new TrieNode[26];
List<String> suggestions = new ArrayList<>();
}
private static TrieNode root = new TrieNode();
public static List<List<String>> suggestedProductsUsingTrieAndSuggestionsArray(String[] products, String searchWord) {
Arrays.sort(products);
// insert
for(String st : products) insert(st);
// search
List<List<String>> result = new ArrayList<>();
TrieNode node = root;
for(char c : searchWord.toCharArray()){
int id = c - 'a';
if(node != null){
node = node.children[id];
}
result.add(node == null ? new ArrayList<>() : node.suggestions);
}
return result;
}
public static void insert(String word){
TrieNode node = root;
for(char c : word.toCharArray()){
int idx = c - 'a';
if(node.children[idx] == null) node.children[idx] = new TrieNode();
node = node.children[idx];
if(node.suggestions.size() < 3) node.suggestions.add(word);
}
}
class Trie3 {
Trie3[] children = new Trie3[26];
List<Integer> v = new ArrayList<>();
public void insert(String w, int i) {
Trie3 node = this;
for (int j = 0; j < w.length(); ++j) {
int idx = w.charAt(j) - 'a';
if (node.children[idx] == null) {
node.children[idx] = new Trie3();
}
node = node.children[idx];
if (node.v.size() < 3) {
node.v.add(i);
}
}
}
@SuppressWarnings("all")
public List<Integer>[] search(String w) {
Trie3 node = this;
int n = w.length();
List<Integer>[] ans = new List[n];
Arrays.setAll(ans, k -> new ArrayList<>());
for (int i = 0; i < n; ++i) {
int idx = w.charAt(i) - 'a';
if (node.children[idx] == null) {
break;
}
node = node.children[idx];
ans[i] = node.v;
}
return ans;
}
}
public List<List<String>> suggestedProducts3(String[] products, String searchWord) {
Arrays.sort(products);
Trie3 trie = new Trie3();
for (int i = 0; i < products.length; ++i) {
trie.insert(products[i], i);
}
List<List<String>> ans = new ArrayList<>();
for (var v : trie.search(searchWord)) {
List<String> t = new ArrayList<>();
for (int i : v) {
t.add(products[i]);
}
ans.add(t);
}
return ans;
}
/**
MY APPROACH
*/
static class Node {
Node[] arr;
boolean isEnd;
Node(){
arr = new Node[26];
isEnd = false;
}
}
static Node rootNode = new Node();
static List<List<String>> res = new ArrayList<>();
public static List<List<String>> suggestedProductsMyApproach(String[] products, String searchWord) {
res = new ArrayList<>();
// prepare trie
prepareTrie(products);
// enter each char
// Node trav = rootNode;
for(int i=0; i<searchWord.length(); i++) {
prefix(searchWord.substring(0, i+1));
// char c = searchWord.charAt(i);
// List<String> lst = new ArrayList<>();
// if(trav.arr[c-'a']!=null) {
// trav=trav.arr[c-'a'];
// findChildren(
// new StringBuilder(searchWord.substring(0,i+1)),
// trav, lst
// );
// }
// res.add(lst);
}
return res;
}
private static void prepareTrie(String[] products) {
for(String product: products) {
Node trav = rootNode;
for(char c: product.toCharArray()) {
if(trav.arr[c-'a'] == null) trav.arr[c-'a'] = new Node();
trav = trav.arr[c-'a'];
}
trav.isEnd=true;
}
}
private static void prefix(String str) {
List<String> lst = new ArrayList<>();
Node trav = rootNode;
for(char c : str.toCharArray()) {
if(trav.arr[c-'a']==null) {
res.add(lst);
return;
}
trav = trav.arr[c-'a'];
}
// find 3 children with isEnd=true;
findChildren(new StringBuilder(str), trav, lst); // dfs
res.add(lst);
}
private static void findChildren(StringBuilder sb, Node trav, List<String> lst) { // dfs
if(lst.size()==3) return;
if(trav.isEnd) lst.add(sb.toString());
// for loop
for(int i=0; i<26; i++) {
if(trav.arr[i]!=null) {
findChildren(sb.append((char)('a'+i)), trav.arr[i], lst);
sb.deleteCharAt(sb.length() - 1); // or sb.delete(sb.length() - 1, sb.length());
// to revert the change
}
}
}
}