Skip to content

Commit 5f720f2

Browse files
refactor 884
1 parent 1a02ecd commit 5f720f2

File tree

1 file changed

+18
-47
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+18
-47
lines changed

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

+18-47
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,26 @@
55
import java.util.List;
66
import java.util.Map;
77

8-
/**
9-
* 884. Uncommon Words from Two Sentences
10-
*
11-
* We are given two sentences A and B. (A sentence is a string of space separated words. Each word consists only of lowercase letters.)
12-
*
13-
* A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
14-
*
15-
* Return a list of all uncommon words.
16-
*
17-
* You may return the list in any order.
18-
*
19-
*
20-
*
21-
* Example 1:
22-
*
23-
* Input: A = "this apple is sweet", B = "this apple is sour"
24-
* Output: ["sweet","sour"]
25-
* Example 2:
26-
*
27-
* Input: A = "apple apple", B = "banana"
28-
* Output: ["banana"]
29-
*
30-
*
31-
* Note:
32-
*
33-
* 0 <= A.length <= 200
34-
* 0 <= B.length <= 200
35-
* A and B both contain only spaces and lowercase letters.
36-
*/
378
public class _884 {
38-
public static class Solution1 {
39-
public String[] uncommonFromSentences(String A, String B) {
40-
Map<String, Integer> map = new HashMap<>();
41-
for (String word : A.split(" ")) {
42-
map.put(word, map.getOrDefault(word, 0) + 1);
43-
}
9+
public static class Solution1 {
10+
public String[] uncommonFromSentences(String A, String B) {
11+
Map<String, Integer> map = new HashMap<>();
12+
for (String word : A.split(" ")) {
13+
map.put(word, map.getOrDefault(word, 0) + 1);
14+
}
4415

45-
for (String word : B.split(" ")) {
46-
map.put(word, map.getOrDefault(word, 0) + 1);
47-
}
48-
List<String> result = new ArrayList<>();
49-
for (String key : map.keySet()) {
50-
if (map.get(key) == 1) {
51-
result.add(key);
16+
for (String word : B.split(" ")) {
17+
map.put(word, map.getOrDefault(word, 0) + 1);
18+
}
19+
List<String> result = new ArrayList<>();
20+
for (String key : map.keySet()) {
21+
if (map.get(key) == 1) {
22+
result.add(key);
23+
}
24+
}
25+
String[] strs = new String[result.size()];
26+
result.toArray(strs);
27+
return strs;
5228
}
53-
}
54-
String[] strs = new String[result.size()];
55-
result.toArray(strs);
56-
return strs;
5729
}
58-
}
5930
}

0 commit comments

Comments
 (0)