@@ -63,55 +63,58 @@ List<String> input(char c): The input c is the next character typed by the user.
63
63
Please remember to RESET your class variables declared in class AutocompleteSystem, as static/class variables are persisted across multiple test cases. Please see here for more details.
64
64
*/
65
65
public class _642 {
66
+ public static class Solution1 {
66
67
67
- /**reference: https://discuss.leetcode.com/topic/96150/java-solution-trie-and-priorityqueue/3*/
68
- public class AutocompleteSystem {
68
+ /**
69
+ * reference: https://discuss.leetcode.com/topic/96150/java-solution-trie-and-priorityqueue/3
70
+ */
71
+ public class AutocompleteSystem {
69
72
70
- Map <String ,Integer >map ;
71
- List <Map .Entry <String ,Integer >>answers ;
72
- StringBuilder stringBuilder ;
73
+ Map <String ,Integer >map ;
74
+ List <Map .Entry <String ,Integer >>answers ;
75
+ StringBuilder stringBuilder ;
73
76
74
- public AutocompleteSystem (String []sentences ,int []times ) {
75
- map =new HashMap <>();
76
- answers =new ArrayList <>();
77
- stringBuilder =new StringBuilder ();
77
+ public AutocompleteSystem (String []sentences ,int []times ) {
78
+ map =new HashMap <>();
79
+ answers =new ArrayList <>();
80
+ stringBuilder =new StringBuilder ();
78
81
79
- for (int i =0 ;i <sentences .length ;i ++) {
80
- map .put (sentences [i ],map .getOrDefault (sentences [i ],0 ) +times [i ]);
82
+ for (int i =0 ;i <sentences .length ;i ++) {
83
+ map .put (sentences [i ],map .getOrDefault (sentences [i ],0 ) +times [i ]);
84
+ }
81
85
}
82
- }
83
86
84
- public List <String >input (char c ) {
85
- List <String >result =new ArrayList <>();
86
- if (c =='#' ) {
87
- map .put (stringBuilder .toString (),map .getOrDefault (stringBuilder .toString (),0 ) +1 );
88
- stringBuilder .setLength (0 );
89
- answers .clear ();/**The user has finished typing, so we'll clean answers to get ready for next search*/
90
- }else {
91
- stringBuilder .append (c );
92
- /**when its length is 1, we find all the prefix that is a match and put them into answers,
93
- * then for the rest, we'll just remove those that are not match with the prefix any more, we do this logic in else branch*/
94
- if (stringBuilder .length () ==1 ) {
95
- for (Map .Entry <String ,Integer >entry :map .entrySet ()) {
96
- if (entry .getKey ().startsWith (stringBuilder .toString ())) {
97
- answers .add (entry );
98
- }
99
- }
100
- Collections .sort (answers , (a ,b ) ->a .getValue () ==b .getValue () ?a .getKey ().compareTo (b .getKey ()) :b .getValue () -a .getValue ());
87
+ public List <String >input (char c ) {
88
+ List <String >result =new ArrayList <>();
89
+ if (c =='#' ) {
90
+ map .put (stringBuilder .toString (),map .getOrDefault (stringBuilder .toString (),0 ) +1 );
91
+ stringBuilder .setLength (0 );
92
+ answers .clear ();/**The user has finished typing, so we'll clean answers to get ready for next search*/
101
93
}else {
102
- for (Iterator <Map .Entry <String ,Integer >>iterator =answers .iterator ();iterator .hasNext ();) {
103
- if (!iterator .next ().getKey ().startsWith (stringBuilder .toString ())) {
104
- iterator .remove ();
94
+ stringBuilder .append (c );
95
+ /**when its length is 1, we find all the prefix that is a match and put them into answers,
96
+ * then for the rest, we'll just remove those that are not match with the prefix any more, we do this logic in else branch*/
97
+ if (stringBuilder .length () ==1 ) {
98
+ for (Map .Entry <String ,Integer >entry :map .entrySet ()) {
99
+ if (entry .getKey ().startsWith (stringBuilder .toString ())) {
100
+ answers .add (entry );
101
+ }
102
+ }
103
+ Collections .sort (answers , (a ,b ) ->a .getValue () ==b .getValue () ?a .getKey ().compareTo (b .getKey ()) :b .getValue () -a .getValue ());
104
+ }else {
105
+ for (Iterator <Map .Entry <String ,Integer >>iterator =answers .iterator ();iterator .hasNext (); ) {
106
+ if (!iterator .next ().getKey ().startsWith (stringBuilder .toString ())) {
107
+ iterator .remove ();
108
+ }
105
109
}
106
110
}
111
+ for (int i =0 ;i <3 &&i <answers .size ();i ++) {
112
+ result .add (answers .get (i ).getKey ());
113
+ }
107
114
}
108
-
109
- for (int i =0 ;i <3 &&i <answers .size ();i ++) {
110
- result .add (answers .get (i ).getKey ());
111
- }
115
+ return result ;
112
116
}
113
- return result ;
114
117
}
115
- }
116
118
119
+ }
117
120
}