使用map存儲每個詞的次數,遍歷兩句話,次數爲1的即爲不常見的詞。
其中判斷是否等於空格的十進制數32,若等於,則說明一個詞結束了。
vector<string> uncommonFromSentences(string A, string B) {
map<string,int> mymap;
int pos = 0 ;
for(int i = 0; i < A.size(); i ++)
{
if(A[i] == 32 )
{
mymap[A.substr(pos, i-pos)]++;
///cout << A.substr(pos,i)<< endl;
if((i+1) != A.size())
pos = i+1;
}
if(i+1 == A.size())
mymap[A.substr(pos, i-pos+1)]++;
}
pos = 0;
for(int j = 0; j < B.size(); j++)
{
if(B[j] == 32)
{
mymap[B.substr(pos, j-pos)] ++ ;
if(j+1 != B.size() )
pos = j + 1;
}
if(j+1 == B.size())
mymap[B.substr(pos, j-pos+1)]++;
}
vector<string> res;
map<string,int>::iterator it = mymap.begin();
for(it = mymap.begin(); it != mymap.end() ; it ++)
{
//cout << it-> first<<endl;
if(it->second == 1)
{
res.push_back(it->first);
}
}
return res;
}