-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1268.cpp
More file actions
24 lines (21 loc) · 719 Bytes
/
Copy path1268.cpp
File metadata and controls
24 lines (21 loc) · 719 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
vector<vector<string>> res;
string prefix = "";
auto i = products.begin(), it = i;
int k;
sort(products.begin(), products.end());
for (auto ch : searchWord) {
vector<string> tmp;
prefix += ch;
i = lower_bound(i, products.end(), prefix);
for (it = i, k = 0; it != products.end() && k < 3; k++, it++) {
if ((*it).find(prefix) == -1) break;
tmp.push_back(*it);
}
res.push_back(tmp);
}
return res;
}
};