[leetcode-884-Uncommon Words from Two Sentences]

时间:2021-09-02 14:43:36

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.

思路:

直接利用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 ==)ret.push_back(it->first);
}
return ret;
}