@@ -134,4 +134,88 @@ Note:
134
134
* AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
135
135
* List<String> param_1 = obj.input(c);
136
136
*/
137
- ```
137
+ ```
138
+
139
+ ### Amazon session
140
+ * Method 1 : Trie Tree + HashMap + PriorityQueue
141
+ ```Java
142
+ class AutocompleteSystem {
143
+ private static final class Node {
144
+ Map<Character , Node > childs; // key: next character, value: child node.
145
+ Map<String , Integer > freq; // key: sentence, value: appear number.
146
+ public Node (){
147
+ this . childs = new HashMap<> ();
148
+ this . freq = new HashMap<> ();
149
+ }
150
+ }
151
+ private Node root;
152
+ private void addToTree (String s , int time ){
153
+ char [] arr = s. toCharArray();
154
+ Node temp = root;
155
+ for (char c : arr){
156
+ if (! temp. childs. containsKey(c)){
157
+ temp. childs. put(c, new Node ());
158
+ }
159
+ temp = temp. childs. get(c);
160
+ int appear = temp. freq. getOrDefault(s, 0 );
161
+ temp. freq. put(s, appear + time);
162
+ }
163
+ }
164
+ private StringBuilder sb;
165
+ private Node search;
166
+ public AutocompleteSystem (String [] sentences , int [] times ) {
167
+ this . root = new Node ();
168
+ for (int i = 0 ; i < sentences. length; i++ ){
169
+ addToTree(sentences[i], times[i]);
170
+ }
171
+ this . sb = new StringBuilder ();
172
+ this . search = this . root;
173
+ }
174
+ private static class Pair {
175
+ int freq;
176
+ String sentence;
177
+ public Pair (int freq , String sentence ){
178
+ this . freq = freq;
179
+ this . sentence = sentence;
180
+ }
181
+ }
182
+ private List<String > getTopFromNode (Node node ){
183
+ List<String > result = new ArrayList<> ();
184
+ PriorityQueue<Pair > pq = new PriorityQueue<> ((a, b)- > {
185
+ if (b. freq != a. freq) return b. freq - a. freq;
186
+ else return a. sentence. compareTo(b. sentence);
187
+ });
188
+ for (Map . Entry<String , Integer > entry: node. freq. entrySet()){
189
+ pq. offer(new Pair (entry. getValue(), entry. getKey()));
190
+ }
191
+ while (! pq. isEmpty() && result. size() < 3 ){
192
+ result. add(pq. poll(). sentence);
193
+ }
194
+ return result;
195
+ }
196
+ public List<String > input (char c ) {
197
+ List<String > result = new ArrayList<> ();
198
+ if (c != ' #' ) sb. append(c);
199
+ if (search != null && c != ' #' ){
200
+ if (search. childs. containsKey(c)){ // normal character and is not #.
201
+ search = search. childs. get(c);
202
+ result = getTopFromNode(search);
203
+ }else { // doesn't contains current node.
204
+ search = null ;
205
+ }
206
+ }
207
+ if (c == ' #' ){
208
+ addToTree(sb. toString(), 1 );
209
+ sb = new StringBuilder ();
210
+ search = this . root;
211
+ }
212
+ return result; // if parent node hasn't existed
213
+ }
214
+ }
215
+
216
+ /**
217
+ * Your AutocompleteSystem object will be instantiated and called as such:
218
+ * AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
219
+ * List<String> param_1 = obj.input(c);
220
+ */
221
+ ```
0 commit comments