Posted on

Description

Submission

class Solution {

    void parseString(string& s, unordered_map<string, int>& Map) {
        for(auto it1 = s.begin(), it2 = s.begin(); it2 != s.end(); ) {
            it2 = find(it1, s.end(), ' ');
            Map[s.substr(it1 - s.begin(), it2 - it1)]++;
            if(it2 != s.end()) {
                it1 = next(it2);
            }
        }
    }
public:
    vector<string> uncommonFromSentences(string s1, string s2) {
        unordered_map<string, int> m1, m2;
        parseString(s1, m1);
        parseString(s2, m2);

        vector<string> rets;
        for(auto& [str, cnt]: m1) {
            if(cnt == 1 && m2.find(str) == m2.end()) rets.push_back(str);
        }

        for(auto& [str, cnt]: m2) {
            if(cnt == 1 && m1.find(str) == m1.end()) rets.push_back(str);
        }
        return rets;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *