解题思路
代码
力扣链接
官方题解
哈希表 代码
class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
String[] str1 = s1.split(" ");
String[] str2 = s2.split(" ");
Map map1 = new HashMap<>();
Map map2 = new HashMap<>();
for (String s : str1) {
map1.put(s, map1.getOrDefault(s, 0) + 1);
}
for (String s : str2) {
map2.put(s, map2.getOrDefault(s, 0) + 1);
}
List res = new ArrayList<>();
for (String s : map1.keySet()) {
if (map1.get(s) == 1 && !map2.containsKey(s)) {
res.add(s);
}
}
for (String s : map2.keySet()) {
if (map2.get(s) == 1 && !map1.containsKey(s)) {
res.add(s);
}
}
return res.toArray(new String[0]);
}
}



