-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSearch Suggestions System.cpp
48 lines (48 loc) · 1.26 KB
/
Search Suggestions System.cpp
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
43
44
45
46
47
48
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
map<int, vector<string>> m;
vector<vector<string>> res;
for (int i = 0; i < products.size(); i++)
{
int j = 0;
while (products[i][j] == searchWord[j] && j < searchWord.size())
{
if (m.count(j) == 0)
{
vector<string> v;
v.push_back(products[i]);
m.insert(make_pair(j, v));
}
else
{
m[j].push_back(products[i]);
}
j++;
}
}
for (int i = 0; i < searchWord.size(); i++)
{
if (i < m.size())
{
sort(m[i].begin(), m[i].end());
int a;
if (3 <= m[i].size())
{
a = 3;
}
else
{
a = m[i].size();
}
m[i].resize(a);
res.push_back(m[i]);
}
else
{
res.push_back({});
}
}
return res;
}
};