-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLC_1268.cpp
52 lines (52 loc) · 1.64 KB
/
LC_1268.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
49
50
51
52
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
//map<string,int>mp;
priority_queue<string>pq;
int i,sz=products.size(),len=searchWord.length(),j;
vector<vector<string>>res;
string temp="";
for(i=0;i<len;i++)
{
temp=temp+searchWord[i];
//mp.clear();
pq = priority_queue<string>();
for(j=0;j<sz;j++)
{
if(products[j].substr(0,i+1) == temp)
{
//if(mp.size()<3)
// mp[products[j]]=1;
if(pq.size()<3)
pq.push(products[j]);
else if(pq.size()==3)
{
// auto itr = mp.end();
// --itr;
// if (itr->first>products[j])
// {
// mp.erase(itr);
// mp[products[j]]=1;
// }
if(pq.top()>products[j])
{
pq.pop();
pq.push(products[j]);
}
}
}
}
vector<string>t;
// for(auto it=mp.begin();it!=mp.end();it++)
// t.push_back(it->first);
while(!pq.empty())
{
t.push_back(pq.top());
pq.pop();
}
reverse(t.begin(),t.end());
res.push_back(t);
}
return res;
}
};