-
Notifications
You must be signed in to change notification settings - Fork 1
/
1268_Search_Suggestions_System.cpp
61 lines (43 loc) · 1.32 KB
/
1268_Search_Suggestions_System.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// https://leetcode.com/problems/search-suggestions-system/
// https://leetcode.com/problems/search-suggestions-system/discuss/2171348/Simple-Brute-Force-or-Without-Trie-or-Sort-C%2B%2B
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<string>> suggestedProducts(vector<string> &products, string searchWord)
{
vector<vector<string>> search;
sort(products.begin(), products.end());
string temp = "";
for (int i = 0; i < searchWord.length(); ++i)
{
temp.push_back(searchWord[i]); // adding character one by one, to search
vector<string> v;
int cnt = 0;
for (const string &a : products)
{
if (cnt == 3) // we need 3 words at max
break;
if (a.find(temp) == 0) // if string `starts with` temp
{
++cnt;
v.push_back(a);
}
}
search.push_back(v);
}
return search;
}
int main()
{
vector<string> products = {"mobile", "mouse", "moneypot", "monitor", "mousepad"};
string searchWord = "mouse";
vector<vector<string>> search = suggestedProducts(products, searchWord);
for (vector<string> &v : search)
{
for (string &a : v)
cout << a << " ";
cout << "\n";
}
return 0;
}