forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_884.java
30 lines (27 loc) · 904 Bytes
/
_884.java
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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _884 {
public static class Solution1 {
public String[] uncommonFromSentences(String A, String B) {
Map<String, Integer> map = new HashMap<>();
for (String word : A.split(" ")) {
map.put(word, map.getOrDefault(word, 0) + 1);
}
for (String word : B.split(" ")) {
map.put(word, map.getOrDefault(word, 0) + 1);
}
List<String> result = new ArrayList<>();
for (String key : map.keySet()) {
if (map.get(key) == 1) {
result.add(key);
}
}
String[] strs = new String[result.size()];
result.toArray(strs);
return strs;
}
}
}