#include using namespace std; vector> getProductSuggestions(vector& products, const string& search) { // Sort products lexicographically sort(products.begin(), products.end()); vector> results; string prefix = ""; // Build up the prefix from each character in 'search' for (char ch : search) { prefix += ch; vector matches; // Using binary search to optimize finding the start of matching products auto it = lower_bound(products.begin(), products.end(), prefix); // Collect up to 3 matches that start with the current prefix for (int i = 0; i < 3 && it + i != products.end(); i++) { string& product = *(it + i); if (product.find(prefix) == 0) { // starts with 'prefix' matches.push_back(product); } else { break; } } // Add matches for this prefix to results results.push_back(matches); } return results; } int main() { vector products = {"mobile", "mouse", "moneypot", "monitor", "mousepad"}; string search = "mouse"; vector> result = getProductSuggestions(products, search); // Display results for (const auto& suggestions : result) { for (const auto& suggestion : suggestions) { cout << suggestion << " "; } cout << endl; } return 0; }