Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<vector<string>> getProductSuggestions(vector<string> products, string search) {
- sort(products.begin(), products.end());
- vector<vector<string>> result;
- string prefix = "";
- for (char ch : search) {
- prefix += ch;
- vector<string> suggestions;
- auto it = lower_bound(products.begin(), products.end(), prefix);
- for (int i = 0; i < 3 && it + i != products.end(); i++) {
- string& product = *(it + i);
- if (product.substr(0, prefix.size()) == prefix) {
- suggestions.push_back(product);
- } else {
- break;
- }
- }
- result.push_back(suggestions);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement