-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap884.java
More file actions
41 lines (38 loc) · 1.07 KB
/
Map884.java
File metadata and controls
41 lines (38 loc) · 1.07 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package map;
import java.util.*;
import java.util.stream.Collectors;
/**
* @ProjectName: leetcode
* @Package: map
* @ClassName: Map884
* @Author: markey
* @Description:
* @Date: 2020/6/3 22:23
* @Version: 1.0
*/
public class Map884 {
public String[] uncommonFromSentences(String A, String B) {
String[] a = A.split(" ");
Map<String, Integer> mapA = new HashMap<>();
for(String s: a) {
mapA.put(s, mapA.getOrDefault(s, 0) + 1);
}
String[] b = B.split(" ");
Map<String, Integer> mapB = new HashMap<>();
for(String s: b) {
mapB.put(s, mapB.getOrDefault(s, 0) + 1);
}
List<String> list = new ArrayList<>();
for (String key : mapA.keySet()) {
if (mapA.get(key) == 1 && !mapB.containsKey(key)) {
list.add(key);
}
}
for (String key : mapB.keySet()) {
if (mapB.get(key) == 1 && !mapA.containsKey(key)) {
list.add(key);
}
}
return list.toArray(new String[list.size()]);
}
}