We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c0aeeed commit 3b79979Copy full SHA for 3b79979
java/1268-search-suggestions-system.java
@@ -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