Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
- map<string, vector<string>> list;
- for(auto& word : products){
- string prefix;
- for(auto& letter : word){
- prefix += letter;
- list[prefix].emplace_back(word);
- }
- }
- vector<vector<string>> ans;
- string prefix;
- for(auto& letter : searchWord){
- prefix += letter;
- auto& curr = list[prefix];
- sort(curr.begin(), curr.end());
- vector<string> tmp;
- for(int i = 0; i < min(3, (int)curr.size()); i++){
- tmp.emplace_back(curr[i]);
- }
- ans.emplace_back(tmp);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement