-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDesign_Search_Autocomplete_System.cpp
More file actions
116 lines (94 loc) · 2.62 KB
/
Design_Search_Autocomplete_System.cpp
File metadata and controls
116 lines (94 loc) · 2.62 KB
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
// https://leetcode.com/problems/design-search-autocomplete-system/
#define hashmap unordered_map<char, node*>
struct compare{
bool operator() (pair < string, int > &p1, pair < string, int > &p2){
if(p1.second == p2.second)
return p1.first > p2.first;
return p1.second < p2.second;
}
};
class node{
public:
char data;
string str;
hashmap h;
int freq;
bool isTerminal;
node(char d){
data = d;
str = "";
isTerminal = false;
freq = 0;
}
};
class Trie{
public:
node *root;
Trie(){
root = new node('\0');
}
void addWord(string word, int cnt){
node *temp = root;
for(int i = 0; i < word.length(); i++){
char ch = word[i];
if(temp->h.count(ch))
temp = temp->h[ch];
else{
node *child = new node(ch);
child->str = temp->str + ch;
temp->h[ch] = child;
temp = child;
}
}
temp->isTerminal = true;
temp->freq += cnt;
}
};
class AutocompleteSystem {
public:
string curr;
Trie *t;
priority_queue < pair < string, int >, vector < pair < string, int > >, compare > pq;
AutocompleteSystem(vector<string>& sentences, vector<int>& times) {
t = new Trie();
curr = "";
for(int i = 0; i < sentences.size(); i++)
t->addWord(sentences[i], times[i]);
}
void dfs(node* temp){
if (temp->isTerminal)
pq.push({temp->str, temp->freq});
for (auto ele: temp->h){
dfs(ele.second);
}
}
vector<string> input(char c) {
pq = priority_queue < pair < string, int >, vector < pair < string, int > >, compare >();
vector < string > ans;
if(c == '#'){
t->addWord(curr, 1);
curr = "";
return ans;
}
curr += c;
node *temp = t->root;
for(int i = 0; i < curr.length(); i++){
char ch = curr[i];
if(temp->h.count(ch))
temp = temp->h[ch];
else
return ans;
}
dfs(temp);
while (!pq.empty() && ans.size() < 3){
ans.push_back(pq.top().first);
pq.pop();
}
return ans;
}
};
/**
* Your AutocompleteSystem object will be instantiated and called as such:
* AutocompleteSystem* obj = new AutocompleteSystem(sentences, times);
* vector<string> param_1 = obj->input(c);
*/