-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-SearchSuggestionSystem.cpp
More file actions
42 lines (28 loc) · 1.04 KB
/
LeetCode-SearchSuggestionSystem.cpp
File metadata and controls
42 lines (28 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
sort(products.begin(), products.end());
int n = products.size();
list<int> indices;
for (int i = 0; i < n; ++i) indices.push_back(i);
vector<vector<string>> ans;
for (int c = 0; c < searchWord.size(); ++c) {
vector<string> v;
auto it = indices.begin();
while (it != indices.end()) {
int i = *it;
const string& p = products[i];
if (searchWord[c] == p[c]) {
if (v.size() < 3) {
v.push_back(p);
}
++it;
} else {
indices.erase(it++);
}
}
ans.push_back(v);
}
return ans;
}
};