##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/884.html

# 884. Uncommon Words from Two Sentences (Easy)

We are given two sentences A and B.  (A sentence is a string of space separated words.  Each word consists only of lowercase letters.)

A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.

Return a list of all uncommon words.

You may return the list in any order.

Example 1:

Input: A = "this apple is sweet", B = "this apple is sour"
Output: ["sweet","sour"]

Example 2:

Input: A = "apple apple", B = "banana"
Output: ["banana"]

Note:

1. 0 <= A.length <= 200
2. 0 <= B.length <= 200
3. A and B both contain only spaces and lowercase letters.

Related Topics:
Hash Table

## Solution 1.

• class Solution {
public String[] uncommonFromSentences(String A, String B) {
List<String> uncommonWords = new ArrayList<String>();
Set<String> set = new HashSet<String>();
String[] strA = A.split(" ");
String[] strB = B.split(" ");
for (String word : strA) {
else
uncommonWords.remove(word);
}
for (String word : strB) {
else
uncommonWords.remove(word);
}
int length = uncommonWords.size();
String[] uncommonWordsArray = new String[length];
for (int i = 0; i < length; i++)
uncommonWordsArray[i] = uncommonWords.get(i);
return uncommonWordsArray;
}
}

############

class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
Map<String, Integer> cnt = new HashMap<>();
for (String s : s1.split(" ")) {
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
}
for (String s : s2.split(" ")) {
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
}
List<String> ans = new ArrayList<>();
for (var e : cnt.entrySet()) {
if (e.getValue() == 1) {
}
}
return ans.toArray(new String[0]);
}
}

• // OJ: https://leetcode.com/problems/uncommon-words-from-two-sentences/
// Time: O(A+B)
// Space: O(A+B)
class Solution {
private:
unordered_map<string, int> getCounts(string s) {
istringstream ss(s);
string word;
unordered_map<string, int> m;
while (ss >> word) m[word]++;
return m;
}
public:
vector<string> uncommonFromSentences(string A, string B) {
auto m = getCounts(A), n = getCounts(B);
vector<string> ans;
for (auto &p : m) {
if (p.second == 1 && n.find(p.first) == n.end()) ans.push_back(p.first);
}
for (auto &p : n) {
if (p.second == 1 && m.find(p.first) == m.end()) ans.push_back(p.first);
}
return ans;
}
};

• class Solution:
def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
cnt = Counter(s1.split()) + Counter(s2.split())
return [s for s, v in cnt.items() if v == 1]

############
3
class Solution:
def uncommonFromSentences(self, A, B):
"""
:type A: str
:type B: str
:rtype: List[str]
"""
count_A = collections.Counter(A.split(' '))
count_B = collections.Counter(B.split(' '))
words = list((count_A.keys() | count_B.keys()) - (count_A.keys() & count_B.keys()))
ans = []
for word in words:
if count_A[word] == 1 or count_B[word] == 1:
ans.append(word)
return ans

• func uncommonFromSentences(s1 string, s2 string) (ans []string) {
cnt := map[string]int{}
for _, s := range strings.Split(s1, " ") {
cnt[s]++
}
for _, s := range strings.Split(s2, " ") {
cnt[s]++
}
for s, v := range cnt {
if v == 1 {
ans = append(ans, s)
}
}
return
}

• function uncommonFromSentences(s1: string, s2: string): string[] {
const cnt: Map<string, number> = new Map();
for (const s of [...s1.split(' '), ...s2.split(' ')]) {
cnt.set(s, (cnt.get(s) || 0) + 1);
}
const ans: Array<string> = [];
for (const [s, v] of cnt.entries()) {
if (v == 1) {
ans.push(s);
}
}
return ans;
}

• /**
* @param {string} s1
* @param {string} s2
* @return {string[]}
*/
var uncommonFromSentences = function (s1, s2) {
const cnt = new Map();
for (const s of [...s1.split(' '), ...s2.split(' ')]) {
cnt.set(s, (cnt.get(s) || 0) + 1);
}
const ans = [];
for (const [s, v] of cnt.entries()) {
if (v == 1) {
ans.push(s);
}
}
return ans;
};

• use std::collections::HashMap;

impl Solution {
pub fn uncommon_from_sentences(s1: String, s2: String) -> Vec<String> {
let mut map = HashMap::new();
for s in s1.split(' ') {
map.insert(s, !map.contains_key(s));
}
for s in s2.split(' ') {
map.insert(s, !map.contains_key(s));
}
let mut res = Vec::new();
for (k, v) in map {
if v {
res.push(String::from(k))
}
}
res
}
}