-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy path1268.cpp
More file actions
19 lines (18 loc) · 699 Bytes
/
1268.cpp
File metadata and controls
19 lines (18 loc) · 699 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution
{
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord)
{
sort(products.begin(), products.end());
vector<vector<string>> res;
int l = 0, r = products.size() - 1;
for (int i = 0; i < searchWord.size(); i++)
{
while (l <= r && (products[l].size() <= i || products[l][i] != searchWord[i])) l++;
while (l <= r && (products[r].size() <= i || products[r][i] != searchWord[i])) r--;
if (l <= r) res.push_back(vector<string>(products.begin() + l, products.begin() + min(l+3, r+1)));
else res.push_back({});
}
return res;
}
};