-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDesignSearchAutoCompleteSystem.java
191 lines (144 loc) · 5 KB
/
DesignSearchAutoCompleteSystem.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package problems.leetcode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* https://leetcode.com/problems/design-search-autocomplete-system/
*/
public class DesignSearchAutoCompleteSystem {
private static class Trie {
static final int CHAR_COUNT = 'z' - 'a' + 2;
final Node head = new Node('$', new Node[CHAR_COUNT]);
final Map<String, Integer> queries = new HashMap<>();
Trie() {
}
void insert(String text, int increment) {
int newCount = queries.getOrDefault(text, 0) + increment;
queries.put(text, newCount);
Query query = new Query(text, newCount);
Node node = head;
for (int i = 0; i < text.length(); i++) {
node.update(query);
node = node.getOrCreateChildAt(text.charAt(i));
}
node.update(query);
}
}
private static class Node {
char ch;
Node[] children;
List<Query> topQueries = new ArrayList<>();
Node(char ch, Node[] children) {
this.ch = ch;
this.children = children;
}
Node getOrCreateChildAt(char c) {
int i = index(c);
Node child = children[i];
if (child == null) {
child = new Node(c, new Node[Trie.CHAR_COUNT]);
children[i] = child;
}
return child;
}
int index(char c) {
if (c == ' ') {
return 0;
}
return c - 'a' + 1;
}
void update(Query query) {
Iterator<Query> it = topQueries.iterator();
while (it.hasNext()) {
Query q = it.next();
if (q.text.equals(query.text)) {
it.remove();
break;
}
}
topQueries.add(query);
Collections.sort(topQueries);
if (topQueries.size() > AutocompleteSystem.MAX_RESULT_COUNT) {
topQueries.remove(topQueries.size() - 1);
}
}
@Override
public String toString() {
return "Node{" +
"ch=" + ch +
", topQueries=" + topQueries +
'}';
}
}
private static class Query implements Comparable<Query> {
final String text;
final int count;
Query(String text, int count) {
this.text = text;
this.count = count;
}
@Override
public int compareTo(Query other) {
int c = Integer.compare(this.count, other.count);
return c != 0 ? -c : this.text.compareTo(other.text);
}
@Override
public String toString() {
return "Query{" +
"text='" + text + '\'' +
", count=" + count +
'}';
}
}
private static class AutocompleteSystem {
private static final int MAX_RESULT_COUNT = 3;
private Trie trie = new Trie();
private final StringBuilder queryBuilder = new StringBuilder();
private Node node = trie.head;
public AutocompleteSystem(String[] sentences, int[] times) {
for (int i = 0; i < sentences.length; i++) {
trie.insert(sentences[i], times[i]);
}
}
public List<String> input(char c) {
if (c == '#') {
String text = queryBuilder.toString();
trie.insert(text, 1);
queryBuilder.delete(0, text.length());
node = trie.head;
return Collections.emptyList();
}
queryBuilder.append(c);
node = node.getOrCreateChildAt(c);
List<String> queries = new ArrayList<>();
for (Query q : node.topQueries) {
queries.add(q.text);
if (queries.size() == MAX_RESULT_COUNT) {
break;
}
}
return queries;
}
}
public static void main(String[] args) {
String[] sentences = {"i love you", "island", "ironman", "i love leetcode"};
int[] times = {5, 3, 2, 2};
AutocompleteSystem autoComplete = new AutocompleteSystem(sentences, times);
System.out.println(autoComplete.input('i'));
System.out.println(autoComplete.input(' '));
System.out.println(autoComplete.input('a'));
autoComplete.input('#');
System.out.println(autoComplete.input('i'));
System.out.println(autoComplete.input(' '));
System.out.println(autoComplete.input('a'));
autoComplete.input('#');
System.out.println(autoComplete.input('i'));
System.out.println(autoComplete.input(' '));
System.out.println(autoComplete.input('a'));
autoComplete.input('#');
System.out.println();
}
}