Skip to content

Commit baaf2dc

Browse files
committed
feat: add solutions to lc problem: No.0642
No.0642.Design Search Autocomplete System
1 parent deef616 commit baaf2dc

File tree

4 files changed

+437
-4
lines changed

4 files changed

+437
-4
lines changed

solution/0600-0699/0642.Design Search Autocomplete System/README.md

+147-2
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,167 @@ obj.input("#"); // return []. The user finished the input, the sentence "i a" sh
7171

7272
<!-- 这里可写通用的实现逻辑 -->
7373

74+
**方法一:前缀树 + 排序/优先队列**
75+
7476
<!-- tabs:start -->
7577

7678
### **Python3**
7779

7880
<!-- 这里可写当前语言的特殊实现逻辑 -->
7981

8082
```python
81-
83+
class Trie:
84+
def __init__(self):
85+
self.children = [None] * 27
86+
self.v = 0
87+
self.w = ''
88+
89+
def insert(self, w, t):
90+
node = self
91+
for c in w:
92+
idx = 26 if c == ' ' else ord(c) - ord('a')
93+
if node.children[idx] is None:
94+
node.children[idx] = Trie()
95+
node = node.children[idx]
96+
node.v += t
97+
node.w = w
98+
99+
def search(self, pref):
100+
node = self
101+
for c in pref:
102+
idx = 26 if c == ' ' else ord(c) - ord('a')
103+
if node.children[idx] is None:
104+
return None
105+
node = node.children[idx]
106+
return node
107+
108+
109+
class AutocompleteSystem:
110+
111+
def __init__(self, sentences: List[str], times: List[int]):
112+
self.trie = Trie()
113+
for a, b in zip(sentences, times):
114+
self.trie.insert(a, b)
115+
self.t = []
116+
117+
def input(self, c: str) -> List[str]:
118+
def dfs(node):
119+
if node is None:
120+
return
121+
if node.v:
122+
res.append((node.v, node.w))
123+
for nxt in node.children:
124+
dfs(nxt)
125+
126+
if c == '#':
127+
s = ''.join(self.t)
128+
self.trie.insert(s, 1)
129+
self.t = []
130+
return []
131+
132+
res = []
133+
self.t.append(c)
134+
node = self.trie.search(''.join(self.t))
135+
if node is None:
136+
return res
137+
dfs(node)
138+
res.sort(key=lambda x: (-x[0], x[1]))
139+
return [v[1] for v in res[:3]]
140+
141+
142+
# Your AutocompleteSystem object will be instantiated and called as such:
143+
# obj = AutocompleteSystem(sentences, times)
144+
# param_1 = obj.input(c)
82145
```
83146

84147
### **Java**
85148

86149
<!-- 这里可写当前语言的特殊实现逻辑 -->
87150

88151
```java
89-
152+
class Trie {
153+
Trie[] children = new Trie[27];
154+
int v;
155+
String w = "";
156+
157+
void insert(String w, int t) {
158+
Trie node = this;
159+
for (char c : w.toCharArray()) {
160+
int idx = c == ' ' ? 26 : c - 'a';
161+
if (node.children[idx] == null) {
162+
node.children[idx] = new Trie();
163+
}
164+
node = node.children[idx];
165+
}
166+
node.v += t;
167+
node.w = w;
168+
}
169+
170+
Trie search(String pref) {
171+
Trie node = this;
172+
for (char c : pref.toCharArray()) {
173+
int idx = c == ' ' ? 26 : c - 'a';
174+
if (node.children[idx] == null) {
175+
return null;
176+
}
177+
node = node.children[idx];
178+
}
179+
return node;
180+
}
181+
}
182+
183+
class AutocompleteSystem {
184+
private Trie trie = new Trie();
185+
private StringBuilder t = new StringBuilder();
186+
187+
public AutocompleteSystem(String[] sentences, int[] times) {
188+
int i = 0;
189+
for (String s : sentences) {
190+
trie.insert(s, times[i++]);
191+
}
192+
}
193+
194+
public List<String> input(char c) {
195+
List<String> res = new ArrayList<>();
196+
if (c == '#') {
197+
trie.insert(t.toString(), 1);
198+
t = new StringBuilder();
199+
return res;
200+
}
201+
t.append(c);
202+
Trie node = trie.search(t.toString());
203+
if (node == null) {
204+
return res;
205+
}
206+
PriorityQueue<Trie> q = new PriorityQueue<>((a, b) -> a.v == b.v ? b.w.compareTo(a.w) : a.v - b.v);
207+
dfs(node, q);
208+
while (!q.isEmpty()) {
209+
res.add(0, q.poll().w);
210+
}
211+
return res;
212+
}
213+
214+
private void dfs(Trie node, PriorityQueue q) {
215+
if (node == null) {
216+
return;
217+
}
218+
if (node.v > 0) {
219+
q.offer(node);
220+
if (q.size() > 3) {
221+
q.poll();
222+
}
223+
}
224+
for (Trie nxt : node.children) {
225+
dfs(nxt, q);
226+
}
227+
}
228+
}
229+
230+
/**
231+
* Your AutocompleteSystem object will be instantiated and called as such:
232+
* AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
233+
* List<String> param_1 = obj.input(c);
234+
*/
90235
```
91236

92237
### **...**

solution/0600-0699/0642.Design Search Autocomplete System/README_EN.md

+145-2
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,156 @@ obj.input(&quot;#&quot;); // return []. The user finished the input, the sentenc
7070
### **Python3**
7171

7272
```python
73-
73+
class Trie:
74+
def __init__(self):
75+
self.children = [None] * 27
76+
self.v = 0
77+
self.w = ''
78+
79+
def insert(self, w, t):
80+
node = self
81+
for c in w:
82+
idx = 26 if c == ' ' else ord(c) - ord('a')
83+
if node.children[idx] is None:
84+
node.children[idx] = Trie()
85+
node = node.children[idx]
86+
node.v += t
87+
node.w = w
88+
89+
def search(self, pref):
90+
node = self
91+
for c in pref:
92+
idx = 26 if c == ' ' else ord(c) - ord('a')
93+
if node.children[idx] is None:
94+
return None
95+
node = node.children[idx]
96+
return node
97+
98+
99+
class AutocompleteSystem:
100+
101+
def __init__(self, sentences: List[str], times: List[int]):
102+
self.trie = Trie()
103+
for a, b in zip(sentences, times):
104+
self.trie.insert(a, b)
105+
self.t = []
106+
107+
def input(self, c: str) -> List[str]:
108+
def dfs(node):
109+
if node is None:
110+
return
111+
if node.v:
112+
res.append((node.v, node.w))
113+
for nxt in node.children:
114+
dfs(nxt)
115+
116+
if c == '#':
117+
s = ''.join(self.t)
118+
self.trie.insert(s, 1)
119+
self.t = []
120+
return []
121+
122+
res = []
123+
self.t.append(c)
124+
node = self.trie.search(''.join(self.t))
125+
if node is None:
126+
return res
127+
dfs(node)
128+
res.sort(key=lambda x: (-x[0], x[1]))
129+
return [v[1] for v in res[:3]]
130+
131+
132+
# Your AutocompleteSystem object will be instantiated and called as such:
133+
# obj = AutocompleteSystem(sentences, times)
134+
# param_1 = obj.input(c)
74135
```
75136

76137
### **Java**
77138

78139
```java
79-
140+
class Trie {
141+
Trie[] children = new Trie[27];
142+
int v;
143+
String w = "";
144+
145+
void insert(String w, int t) {
146+
Trie node = this;
147+
for (char c : w.toCharArray()) {
148+
int idx = c == ' ' ? 26 : c - 'a';
149+
if (node.children[idx] == null) {
150+
node.children[idx] = new Trie();
151+
}
152+
node = node.children[idx];
153+
}
154+
node.v += t;
155+
node.w = w;
156+
}
157+
158+
Trie search(String pref) {
159+
Trie node = this;
160+
for (char c : pref.toCharArray()) {
161+
int idx = c == ' ' ? 26 : c - 'a';
162+
if (node.children[idx] == null) {
163+
return null;
164+
}
165+
node = node.children[idx];
166+
}
167+
return node;
168+
}
169+
}
170+
171+
class AutocompleteSystem {
172+
private Trie trie = new Trie();
173+
private StringBuilder t = new StringBuilder();
174+
175+
public AutocompleteSystem(String[] sentences, int[] times) {
176+
int i = 0;
177+
for (String s : sentences) {
178+
trie.insert(s, times[i++]);
179+
}
180+
}
181+
182+
public List<String> input(char c) {
183+
List<String> res = new ArrayList<>();
184+
if (c == '#') {
185+
trie.insert(t.toString(), 1);
186+
t = new StringBuilder();
187+
return res;
188+
}
189+
t.append(c);
190+
Trie node = trie.search(t.toString());
191+
if (node == null) {
192+
return res;
193+
}
194+
PriorityQueue<Trie> q = new PriorityQueue<>((a, b) -> a.v == b.v ? b.w.compareTo(a.w) : a.v - b.v);
195+
dfs(node, q);
196+
while (!q.isEmpty()) {
197+
res.add(0, q.poll().w);
198+
}
199+
return res;
200+
}
201+
202+
private void dfs(Trie node, PriorityQueue q) {
203+
if (node == null) {
204+
return;
205+
}
206+
if (node.v > 0) {
207+
q.offer(node);
208+
if (q.size() > 3) {
209+
q.poll();
210+
}
211+
}
212+
for (Trie nxt : node.children) {
213+
dfs(nxt, q);
214+
}
215+
}
216+
}
217+
218+
/**
219+
* Your AutocompleteSystem object will be instantiated and called as such:
220+
* AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
221+
* List<String> param_1 = obj.input(c);
222+
*/
80223
```
81224

82225
### **...**

0 commit comments

Comments
 (0)