-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy path1268.java
More file actions
17 lines (16 loc) · 726 Bytes
/
1268.java
File metadata and controls
17 lines (16 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
Arrays.sort(products);
List<List<String>> res = new ArrayList();
int l = 0, r = products.length - 1;
for (int i = 0; i < searchWord.length(); i++) {
while (l <= r && (products[l].length() <= i || products[l].charAt(i) != searchWord.charAt(i))) l++;
while (l <= r && (products[r].length() <= i || products[r].charAt(i) != searchWord.charAt(i))) r--;
List<String> tmp = new ArrayList();
for (int j = l; j < Math.min(l+3, r+1); j++)
tmp.add(products[j]);
res.add(tmp);
}
return res;
}
}