Leetcode 884. Uncommon Words from Two Sentences | | Leetcode 884. Uncommon Words from Two Sentences 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution Version 1 1234567891011121314151617181920212223242526272829303132333435class 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 12345678910111213141516171819202122class 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; }}; Reference https://leetcode.com/problems/uncommon-words-from-two-sentences/description/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏