文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
Uncommon Words from Two Sentences2. Solution
- Version 1
class Solution {
public:
vector<string> uncommonFromSentences(string A, string B) {
vector<string> result;
unordered_map<string, int> m;
string s;
for(char ch : A) {
if(ch != ' ') {
s += ch;
}
else {
m[s]++;
s = "";
}
}
m[s]++;
s = "";
for(char ch : B) {
if(ch != ' ') {
s += ch;
}
else {
m[s]++;
s = "";
}
}
m[s]++;
for(auto pair : m) {
if(pair.second == 1) {
result.emplace_back(pair.first);
}
}
return result;
}
};
- Version 2
class Solution {
public:
vector<string> uncommonFromSentences(string A, string B) {
vector<string> result;
unordered_map<string, int> m;
istringstream strA(A);
string s;
while(strA >> s) {
m[s]++;
}
istringstream strB(B);
while(strB >> s) {
m[s]++;
}
for(auto pair : m) {
if(pair.second == 1) {
result.emplace_back(pair.first);
}
}
return result;
}
};
网友评论