-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDesignSearchAutocompleteSystem.java
172 lines (145 loc) · 4.95 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
package com.leetcode;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
class AutocompleteSystem {
Trie trie;
String str; // sentence user is typing
List<String> result;
public AutocompleteSystem(String[] sentences, int[] times) {
this.trie = new Trie();
result = new LinkedList<>();
str = "";
for(int i = 0 ; i < sentences.length ; i++){
trie.insert(sentences[i],times[i]);
}
}
public List<String> input(char c) {
//string end record the string before # and return result
if(c == '#'){
trie.insert(str);
str = "";
//return result
}
//this is a bad string do not record
if(!Character.isAlphabetic(c) && c != ' '){
str = "";
return new LinkedList<>();
}
str = str + c;
// no suggestions
if(!trie.contains(str))
return new LinkedList<>();
return trie.top3HotWords(str);
}
class Trie{
TrieNode root;
public Trie(){
root = new TrieNode();
}
//for initial insert by weight
public void insert(String word, int weight){
TrieNode node = this.root;
for(int i = 0 ; i < word.length() ; i++){
char ch = word.charAt(i);
TrieNode next = node.childs.getOrDefault(ch,new TrieNode());
node.childs.put(ch , next);
node = next;
}
node.isWord = true;
node.word = word;
node.weight = weight;
}
//insert to update a historical count - mostly copy of above
//can call this method add
public void insert(String word){
TrieNode node = this.root;
for(int i = 0 ; i < word.length() ; i++){
char ch = word.charAt(i);
TrieNode next = node.childs.getOrDefault(ch,new TrieNode());
node.childs.put(ch , next);
node = next;
}
node.isWord = true;
node.word = word;
node.weight = node.weight+1;
}
//contains prefix
public boolean contains(String prefix){
TrieNode node = this.root;
for(int i = 0 ; i < prefix.length() ; i++){
char ch = prefix.charAt(i);
if(!node.childs.containsKey(ch)) return false;
TrieNode next = node.childs.getOrDefault(ch,new TrieNode());
node = next;
}
return true;
}
//
public List<String> top3HotWords(String prefix){
if(!contains(prefix)) return new LinkedList<>();
TrieNode node = this.root;
for(int i = 0 ; i < prefix.length() ; i++){
char ch = prefix.charAt(i);
TrieNode next = node.childs.getOrDefault(ch,new TrieNode());
node = next;
}
//get all words starting at node
//search all string with prefix and keep them in min Heap for size 3
PriorityQueue<PQNode> minHeap = new PriorityQueue<>((a, b) -> (compare(a,b)));
Queue<TrieNode> queue = new LinkedList<>();
queue.offer(node);//starting root is node
while(queue.size() >0){
Queue<TrieNode> level = new LinkedList<>();
while(queue.size() >0){
node = queue.poll();
if(node.isWord)
minHeap.add(new PQNode(node.word,node.weight));
if(minHeap.size() > 3)
minHeap.poll();
for(TrieNode child : node.childs.values()){
level.offer(child);
}
}
queue = level;
}
List<String> result = new LinkedList<>();
while(minHeap.size() > 0){
PQNode n = minHeap.peek();
result.add(0,n.word);
minHeap.poll();
}
return result;
}
private int compare(PQNode a, PQNode b){
int diff = (a.count - b.count);
if(diff != 0) return diff;
return b.word.compareTo(a.word);//lexi order if count equal
}
}
class TrieNode{
String word;
boolean isWord;
int weight;
HashMap<Character,TrieNode> childs;
public TrieNode(){
childs = new HashMap<>();
word = "";
}
}
class PQNode{
String word;
int count;
public PQNode(String word , int count){
this.word = word;
this.count = count;
}
}
}
/**
* Your AutocompleteSystem object will be instantiated and called as such:
* AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
* List<String> param_1 = obj.input(c);
*/