-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
84 lines (77 loc) · 2.11 KB
/
Solution.java
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
class Trie {
Trie[] children = new Trie[27];
int v;
String w = "";
void insert(String w, int t) {
Trie node = this;
for (char c : w.toCharArray()) {
int idx = c == ' ' ? 26 : c - 'a';
if (node.children[idx] == null) {
node.children[idx] = new Trie();
}
node = node.children[idx];
}
node.v += t;
node.w = w;
}
Trie search(String pref) {
Trie node = this;
for (char c : pref.toCharArray()) {
int idx = c == ' ' ? 26 : c - 'a';
if (node.children[idx] == null) {
return null;
}
node = node.children[idx];
}
return node;
}
}
class AutocompleteSystem {
private Trie trie = new Trie();
private StringBuilder t = new StringBuilder();
public AutocompleteSystem(String[] sentences, int[] times) {
int i = 0;
for (String s : sentences) {
trie.insert(s, times[i++]);
}
}
public List<String> input(char c) {
List<String> res = new ArrayList<>();
if (c == '#') {
trie.insert(t.toString(), 1);
t = new StringBuilder();
return res;
}
t.append(c);
Trie node = trie.search(t.toString());
if (node == null) {
return res;
}
PriorityQueue<Trie> q
= new PriorityQueue<>((a, b) -> a.v == b.v ? b.w.compareTo(a.w) : a.v - b.v);
dfs(node, q);
while (!q.isEmpty()) {
res.add(0, q.poll().w);
}
return res;
}
private void dfs(Trie node, PriorityQueue q) {
if (node == null) {
return;
}
if (node.v > 0) {
q.offer(node);
if (q.size() > 3) {
q.poll();
}
}
for (Trie nxt : node.children) {
dfs(nxt, q);
}
}
}
/**
* Your AutocompleteSystem object will be instantiated and called as such:
* AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
* List<String> param_1 = obj.input(c);
*/