Skip to content

Commit 3b79979

Browse files
committed
1268
1 parent c0aeeed commit 3b79979

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: java/1268-search-suggestions-system.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
3+
List<List<String>> res = new ArrayList<>();
4+
Arrays.sort(products);
5+
6+
int l = 0;
7+
int r = products.length - 1;
8+
for (int i = 0; i < searchWord.length(); i++) {
9+
while (l <= r && (products[l].length() <= i || products[l].charAt(i) != searchWord.charAt(i)))
10+
l++;
11+
while (l <= r && (products[r].length() <= i || products[r].charAt(i) != searchWord.charAt(i)))
12+
r--;
13+
14+
List<String> list = new ArrayList<>();
15+
for (int j = l; j < l + 3 && j <= r; j++) {
16+
list.add(products[j]);
17+
}
18+
res.add(list);
19+
}
20+
return res;
21+
}
22+
}

0 commit comments

Comments
 (0)