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
andB
both contain only spaces and lowercase letters.
思路:
直接利用stringstream 分割处单词来用map统计一下即可。
vector<string> uncommonFromSentences(string A, string B) { vector<string>wordA; stringstream ss(A + " " + B); string t; while(ss>>t){wordA.push_back(t);} map<string,int>mp; for(auto t : wordA){mp[t]++;} vector<string>ret; for(auto it = mp.begin(); it != mp.end(); it++) { if(it->second ==1)ret.push_back(it->first); } return ret; }