Byte by Byte

884. Uncommon Words from Two Sentences.cpp 본문

개발 로그/알고리즘

884. Uncommon Words from Two Sentences.cpp

CyberSoak 2021. 12. 15. 18:41

class Solution {
public:
    vector<string> uncommonFromSentences(string s1, string s2) {
        vector<string>res;
        map<string,int>m;
        for(int i =0; i<s1.size();i++){
            string tmp ="";
            while(1){
                if(s1[i]==' ' || i == s1.size()) break;
                tmp+=s1[i];
                i++;
            }
            // cout<< tmp <<endl;
            m[tmp]++;
            if(s1[i]==' ')continue;
        }
        for(int i =0; i<s2.size();i++){
            string tmp ="";
            while(1){
                if(s2[i]==' ' || i == s2.size()) break;
                tmp+=s2[i];
                i++;
                
            }
            //cout<< tmp <<endl;
            m[tmp]++;
            if(s2[i]==' ') continue;
        }
        for(auto x : m){
            //cout<< x.second<< " "<< x.first <<endl;
            if(x.second ==1)
                res.push_back(x.first);
        }
        return res;
    }
};

'개발 로그 > 알고리즘' 카테고리의 다른 글

1694. Reformat Phone Number.cpp  (0) 2021.12.15
575. Distribute Candies.cpp  (0) 2021.12.15
46. Permutations.cpp  (0) 2021.12.14
118. Pascal's Triangle.cpp  (0) 2021.12.13
2. Add Two Numbers.cpp  (0) 2021.11.26