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:
0 <= A.length <= 200
0 <= B.length <= 200
A and B both contain only spaces and lowercase letters.
/**
* @param {string} A
* @param {string} B
* @return {string[]}
*/
var uncommonFromSentences = function(A, B) {
const aArr = A.split(' ')
const bArr = B.split(' ')
const aLen = aArr.length
const bLen = bArr.length
const map = new Map()
const res = []
if (aLen) {
for (let i = 0; i < aLen; i++) {
map.set(aArr[i], map.has(aArr[i]) ? map.get(aArr[i]) + 1 : 1)
}
}
if (bLen) {
for (let i = 0; i < bLen; i++) {
map.set(bArr[i], map.has(bArr[i]) ? map.get(bArr[i]) + 1 : 1)
}
}
map.forEach((value, word) => {
if (map.get(word) === 1) {
res.push(word)
}
})
return res
};
/**
* @param {string} A
* @param {string} B
* @return {string[]}
*/
var uncommonFromSentences = function(A, B) {
let arr = (A + ' ' + B).split(' ');
return arr.filter(i => arr.indexOf(i) == arr.lastIndexOf(i));
};