Skip to content

Commit b440677

Browse files
refactor 642
1 parent ec0b396 commit b440677

File tree

1 file changed

+41
-38
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+41
-38
lines changed

src/main/java/com/fishercoder/solutions/_642.java

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,55 +63,58 @@ List<String> input(char c): The input c is the next character typed by the user.
6363
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.
6464
*/
6565
public class _642 {
66+
public static class Solution1 {
6667

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 {
6972

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;
7376

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();
7881

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+
}
8185
}
82-
}
8386

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*/
10193
} 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+
}
105109
}
106110
}
111+
for (int i = 0; i < 3 && i < answers.size(); i++) {
112+
result.add(answers.get(i).getKey());
113+
}
107114
}
108-
109-
for (int i = 0; i < 3 && i < answers.size(); i++) {
110-
result.add(answers.get(i).getKey());
111-
}
115+
return result;
112116
}
113-
return result;
114117
}
115-
}
116118

119+
}
117120
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy