-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathSearch_Suggestions_System.cpp
124 lines (99 loc) · 3.33 KB
/
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Company Tags : Amazon, Google (Small variation)
Leetcode Link : Search Suggestions System
*/
//Approach - 1 (First solution that comes in our mind : Using TRIE Data Structure)
class Solution {
public:
struct trieNode {
char ch;
bool end;
trieNode* children[26] = {NULL};
};
trieNode* getNode(char ch) {
trieNode* temp = new trieNode();
temp->end = false;
temp->ch = ch;
return temp;
}
void insert(trieNode* root, string key) {
trieNode* pCrawl = root;
for(char &ch : key) {
int idx = ch-'a';
if(pCrawl->children[idx] == NULL) {
pCrawl->children[idx] = getNode(ch);
}
pCrawl = pCrawl->children[idx];
}
pCrawl->end = true;
}
void getWord(trieNode* pCrawl, string temp, vector<string>& result) {
if(result.size() == 3)
return;
if(pCrawl->end) {
result.push_back(temp);
}
for(char ch = 'a'; ch<='z'; ch++) {
trieNode* nextNode = pCrawl->children[ch-'a'];
if(nextNode) {
temp.push_back(ch);
getWord(nextNode, temp, result);
temp.pop_back();
}
}
}
vector<string> search(trieNode* root, string key) {
trieNode* pCrawl = root;
vector<string> result;
for(char &ch : key) {
int idx = ch-'a';
if(!pCrawl->children[idx]) {
return result;
}
pCrawl = pCrawl->children[idx];
}
getWord(pCrawl, key, result);
return result;
}
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
trieNode* root = getNode('.');
for(string &product : products) {
insert(root, product);
}
vector<vector<string>> result;
int n = searchWord.length();
string prefix = "";
for(int i = 0; i<n; i++) {
prefix.push_back(searchWord[i]);
result.push_back(search(root, prefix));
}
return result;
}
};
//Approach-2 (Since, contraints are low, you can apply Binary Search (Lower bound))
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
vector<vector<string>> result;
int n = products.size();
sort(begin(products), end(products));
string prefix = "";
int index = 0;
for(char &ch : searchWord) {
prefix.push_back(ch);
int start = lower_bound(products.begin() + index, end(products), prefix) - begin(products);
/*
NOTE : lower bound returns element which is equal to or greater than the
search key(prefix here)
*/
result.push_back({});
for(int i = start; i<min(start+3, n); i++) {
if(products[i].find(prefix) < n) {
result.back().push_back(products[i]);
}
}
index = start;
}
return result;
}
};