# 两句话中的不常见单词
import java.util.*;
class Solution884 {
public static void main(String[] args) {
new Solution884().uncommonFromSentences("apple apple", "banana");
}
public String[] uncommonFromSentences(String s1, String s2) {
HashMap<String, Integer> m = new HashMap<String, Integer>();
for (String s : s1.split(" ")) {
m.put(s, m.getOrDefault(s, 0) + 1);
}
for (String s : s2.split(" ")) {
m.put(s, m.getOrDefault(s, 0) + 1);
}
List<String> res = new ArrayList<>();
for (Map.Entry<String, Integer> entry : m.entrySet()) {
if (entry.getValue() == 1) {
res.add(entry.getKey());
}
}
System.out.println(res);
return res.toArray(new String[0]);
}
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25