Uncommon Words from Two Sentences Solutions in C++
Number 884
Difficulty Easy
Acceptance 63.4%
Link LeetCode
Other languages Go
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/uncommon-words-from-two-sentences/description//// Author : liuyubobobo/// Time : 2018-08-11#include <iostream>#include <vector>#include <unordered_map>using namespace std;/// Simulation uncommon words concepts/// Using Two HashMaps////// Time Complexity: O(len(A) + len(B))/// Space Complexity: O(len(A) + len(B))class Solution {public:vector<string> uncommonFromSentences(string A, string B) {unordered_map<string, int> freqA = getFreq(A);unordered_map<string, int> freqB = getFreq(B);vector<string> res;for(const pair<string, int>& p: freqA)if(p.second == 1 && freqB.find(p.first) == freqB.end())res.push_back(p.first);for(const pair<string, int>& p: freqB)if(p.second == 1 && freqA.find(p.first) == freqA.end())res.push_back(p.first);return res;}private:unordered_map<string, int> getFreq(const string& s){unordered_map<string, int> freq;int start = 0;for(int i = start + 1; i <= s.size(); )if(i == s.size() || s[i] == ' '){freq[s.substr(start, i - start)] ++;start = i + 1;i = start + 1;}elsei ++;return freq;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/uncommon-words-from-two-sentences/description//// Author : liuyubobobo/// Time : 2018-08-12#include <iostream>#include <vector>#include <unordered_map>using namespace std;/// Uncommon words only occur once/// Using just one HashMap////// Time Complexity: O(len(A) + len(B))/// Space Complexity: O(len(A) + len(B))class Solution {public:vector<string> uncommonFromSentences(string A, string B) {unordered_map<string, int> freq;getFreq(freq, A);getFreq(freq, B);vector<string> res;for(const pair<string, int>& p: freq)if(p.second == 1)res.push_back(p.first);return res;}private:void getFreq(unordered_map<string, int>& freq, const string& s){int start = 0;for(int i = start + 1; i <= s.size(); )if(i == s.size() || s[i] == ' '){freq[s.substr(start, i - start)] ++;start = i + 1;i = start + 1;}elsei ++;return;}};int main() {return 0;}