package T884两句话中的不常见单词;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Solution {
public String[] uncommonFromSentences(String A, String B) {
A = A + ' ' + B;
String [] a = A.split(" ");
HashMap<String, Integer> m = new HashMap<>();
for (String anA : a) {
m.put(anA, m.getOrDefault(anA, 0)+1);
}
List<String> b = new LinkedList<>();
for (Map.Entry<String, Integer> x : m.entrySet()) {
if (x.getValue() == 1) {
b.add(x.getKey());
}
}
return b.toArray(new String[0]);
}
}
```
import collections
# 统计只出现一次的单词即可
class Solution:
def uncommonFromSentences(self, A: str, B: str):
A =A + ' '+ B
a = collections.Counter(A.split(' '))
for k in list(a.keys()):
if a[k] >1:
a.pop(k)
a = list(a.keys())
return a
if __name__ == '__main__':
x = Solution()
A="apple apple"
B="banana"
print(x.uncommonFromSentences(A,B))