LC 884. Uncommon Words from Two Sentences

Nilanjan Deb · April 6, 2020

This is my CPP solution.

class Solution {
private:
    vector<string> splitStr(string s){
        int n = s.size(),i = 0;
        vector<string> res;
        string store;
        while(i<n){
            if(s[i] == ' '){
                res.push_back(store);
                store.clear();
            }
            else{
                store.push_back(s[i]);
            }
            i++;
        }
        res.push_back(store);
        return res;
    }
public:
    vector<string> uncommonFromSentences(string A, string B) {
        A.push_back(' ');
        A+=B;
        vector<string> exp = splitStr(A);
        unordered_map<string,int> word;
        unordered_map<string,int>::iterator itr;
        for(int i=0;i<exp.size();i++){
            if(word.find(exp[i])==word.end())
                word[exp[i]] = 1;
            else{
                word[exp[i]]++;
            }
        }
        vector<string> ans;
        for(itr = word.begin();itr!=word.end();itr++){
            if(itr->second == 1)
                ans.push_back(itr->first);
        }
        return ans;
    }
};


Dicussion Forum