Advertisement
asdfg0998

dwedwq

Nov 5th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. vector<vector<string>> getProductSuggestions(vector<string> products, string search) {
  2.     sort(products.begin(), products.end());
  3.     vector<vector<string>> result;
  4.     string prefix = "";
  5.  
  6.     for (char ch : search) {
  7.         prefix += ch;
  8.         vector<string> suggestions;
  9.  
  10.         auto it = lower_bound(products.begin(), products.end(), prefix);
  11.  
  12.         for (int i = 0; i < 3 && it + i != products.end(); i++) {
  13.             string& product = *(it + i);
  14.             if (product.substr(0, prefix.size()) == prefix) {
  15.                 suggestions.push_back(product);
  16.             } else {
  17.                 break;
  18.             }
  19.         }
  20.  
  21.         result.push_back(suggestions);
  22.     }
  23.  
  24.     return result;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement