Skip to content

Commit

Permalink
Create 1268.Search-Suggestions-System_v2.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Nov 24, 2019
1 parent 11994fc commit d99a7c9
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord)
{
sort(products.begin(),products.end());
vector<vector<string>>rets;

string word;
for (int i=0; i<searchWord.size(); i++)
{
word.push_back(searchWord[i]);
vector<string>ans;
auto iter = lower_bound(products.begin(),products.end(),word);
for (int k=0; k<3; k++)
{
if (iter==products.end()) break;
if (iter->substr(0,word.size())!=word) break;
ans.push_back(*iter);
iter = next(iter,1);
}
rets.push_back(ans);
}
return rets;
}
};

0 comments on commit d99a7c9

Please sign in to comment.