-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1268-search-suggestions-system.cpp
90 lines (85 loc) · 2.73 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
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
/*
class TrieNode {
public:
TrieNode* bits[26];
bool is_word[26];
bool has(int index) { return bits[index]; }
};
class Trie {
TrieNode* root = new TrieNode();
public:
void insert(const string& s) {
auto itr = root;
for (auto i = 0; i < s.size(); i++) {
int idx = s[i] - 'a';
if (itr->bits[idx] == NULL) itr->bits[idx] = new TrieNode();
if (i == s.size() - 1) itr->is_word[idx] = true;
itr = itr->bits[idx];
}
}
void find(TrieNode* node, int& left, vector<string>& res, string& builder) {
if (left <= 0 || node == NULL) return;
for (int i = 0; i < 26; i++)
if (node->has(i) && left) {
builder.push_back('a' + i);
if (node->is_word[i]) --left, res.push_back(builder);
find(node->bits[i], left, res, builder);
builder.pop_back();
}
}
vector<string> top_three(const string& search_token) {
vector<string> res;
auto itr = root;
for (auto i = 0; i < search_token.size(); i++) {
int idx = search_token[i] - 'a';
if (itr && itr->has(idx)) {
if (i == search_token.size() - 1
&& itr->is_word[idx]) res.push_back("");
itr = itr->bits[idx];
} else return {};
}
int find_count = 3;
string builder = "";
find(itr, find_count, res, builder);
for (int i = 0; i < res.size(); i++) res[i] = search_token + res[i];
return res;
}
};
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& products,
string searchWord) {
vector<vector<string>> res;
auto trie = new Trie();
for (auto product: products) trie->insert(product);
string builder = "";
for (auto search_token: searchWord) {
builder += search_token;
auto top_match_lex = trie->top_three(builder);
res.push_back(top_match_lex);
}
return res;
}
};
*/
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& A, string searchWord) {
auto it = A.begin();
sort(it, A.end());
vector<vector<string>> res;
string cur = "";
for (char c : searchWord) {
cur += c;
vector<string> suggested;
it = lower_bound(it, A.end(), cur);
for (int i = 0; i < 3 && it + i != A.end(); i++) {
string& s = *(it + i);
if (s.find(cur)) break;
suggested.push_back(s);
}
res.push_back(suggested);
}
return res;
}
};