forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search-suggestions-system.cpp
162 lines (149 loc) · 5.22 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Time: ctor: O(n * l), n is the number of products
// , l is the average length of product name
// suggest: O(l^2)
// Space: O(t), t is the number of nodes in trie
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
TrieNode trie;
for (int i = 0; i < products.size(); ++i) {
trie.insert(products, i);
}
auto curr = ≜
vector<vector<string>> result(searchWord.length());
for (int i = 0; i < searchWord.length(); ++i) { // Time: O(l)
if (!curr->leaves.count(searchWord[i])) {
break;
}
curr = curr->leaves[searchWord[i]];
for (const auto& j : curr->infos) {
result[i].emplace_back(products[j]);
}
}
return result;
}
class TrieNode {
public:
static const int TOP_COUNT = 3;
~TrieNode() {
for (auto& kv : leaves) {
if (kv.second) {
delete kv.second;
}
}
}
// Time: O(l)
void insert(const vector<string>& words, int i) {
auto* curr = this;
for (const auto& c : words[i]) {
if (!curr->leaves.count(c)) {
curr->leaves[c] = new TrieNode;
}
curr = curr->leaves[c];
curr->add_info(words, i);
}
}
// Time: O(l)
void add_info(const vector<string>& words, int i) {
infos.emplace_back(i);
sort(infos.begin(), infos.end(),
[&words](const auto& a, const auto& b) {
return words[a] < words[b];
});
if (infos.size() > TOP_COUNT) {
infos.pop_back();
}
}
vector<int> infos;
unordered_map<char, TrieNode *> leaves;
};
};
// Time: ctor: O(n * l * log(n * l)), n is the number of products
// , l is the average length of product name
// suggest: O(l^2)
// Space: O(t), t is the number of nodes in trie
class Solution2 {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
sort(products.begin(), products.end()); // Time: O(n * l * log(n * l))
TrieNode trie;
for (int i = 0; i < products.size(); ++i) {
trie.insert(products, i);
}
auto curr = ≜
vector<vector<string>> result(searchWord.length());
for (int i = 0; i < searchWord.length(); ++i) { // Time: O(l)
if (!curr->leaves.count(searchWord[i])) {
break;
}
curr = curr->leaves[searchWord[i]];
for (const auto& j : curr->infos) {
result[i].emplace_back(products[j]);
}
}
return result;
}
class TrieNode {
public:
static const int TOP_COUNT = 3;
~TrieNode() {
for (auto& kv : leaves) {
if (kv.second) {
delete kv.second;
}
}
}
// Time: O(l)
void insert(const vector<string>& words, int i) {
auto* curr = this;
for (const auto& c : words[i]) {
if (!curr->leaves.count(c)) {
curr->leaves[c] = new TrieNode;
}
curr = curr->leaves[c];
curr->add_info(words, i);
}
}
// Time: O(1)
void add_info(const vector<string>& words, int i) {
if (infos.size() == TOP_COUNT) {
return;
}
infos.emplace_back(i);
}
vector<int> infos;
unordered_map<char, TrieNode *> leaves;
};
};
// Time: ctor: O(n * l * log(n * l)), n is the number of products
// , l is the average length of product name
// suggest: O(l^2 * n)
// Space: O(n * l)
class Solution3 {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
sort(products.begin(), products.end()); // Time: O(n * l * log(n * l))
vector<vector<string>> result;
string prefix;
for (int i = 0; i < searchWord.length(); ++i) { // Time: O(l)
prefix += searchWord[i];
int start = distance(products.cbegin(),
lower_bound(products.cbegin(),
products.cend(),
prefix)); // Time: O(log(n * l))
vector<string> new_products;
for (int j = start; j < products.size(); ++j) { // Time: O(n * l)
if (!(i < products[j].length() && products[j][i] == searchWord[i])) {
break;
}
new_products.emplace_back(products[j]);
}
products = move(new_products);
result.emplace_back();
for (int j = 0; j < min(int(products.size()), 3); ++j) {
result.back().emplace_back(products[j]);
};
}
return result;
}
};