-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesign Search Autocomplete System.java
88 lines (77 loc) · 2.7 KB
/
Design Search Autocomplete System.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
class AutocompleteSystem {
private Trie root = new Trie();
private Trie cur = root;
private String prefix = "";
public AutocompleteSystem(String[] sentences, int[] times) {
for(int i = 0; i < sentences.length; i++) {
store(sentences[i], times[i]);
}
}
public List<String> input(char c) {
// End of input
if(c == '#') {
store(prefix, 1);
prefix = "";
cur = root;
return new ArrayList<>();
}
// Otherwise, search the current node cur
prefix = prefix + c;
// being null means no match in previous iteration
if(cur == null || !cur.children.containsKey(c)) {
cur = null;
return new ArrayList<>();
}
// If found, get count map and push to queue
cur = cur.children.get(c);
Map<String, Integer> counts = cur.counts;
Queue<Pair> heap = new PriorityQueue<>(new Comparator<Pair>() {
public int compare(Pair o1, Pair o2) {
// cnt desc || str asc
return o1.cnt == o2.cnt? o1.s.compareTo(o2.s): o2.cnt - o1.cnt;
}
});
for(String s: counts.keySet()) {
heap.offer(new Pair(s, counts.get(s)));
}
List<String> res = new ArrayList<>();
for(int i = 0; i < 3 && !heap.isEmpty(); i++) {
res.add(heap.poll().s);
}
return res;
}
// -------------- Helpers ------------------
private class Trie {
public Map<String, Integer> counts = new HashMap<>();
public Map<Character, Trie> children = new HashMap<>();
public boolean isWord = false;
}
//** Used in heap */
private class Pair {
public String s;
public Integer cnt;
// String and its count
public Pair(String s, Integer cnt) {
this.s = s;
this.cnt = cnt;
}
}
//** Store sentence in trie */
private void store(String s, int cnt) {
Trie node = root;
for(char c: s.toCharArray()) {
Trie next = node.children.get(c);
if(next == null) next = new Trie();
// Accumulate the counts of s
next.counts.put(s, next.counts.getOrDefault(s, 0) + cnt);
node.children.put(c, next);
node = next;
}
node.isWord = true;
}
}
/**
* Your AutocompleteSystem object will be instantiated and called as such:
* AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
* List<String> param_1 = obj.input(c);
*/