Krononberg

890. Find and Replace Pattern.cpp 본문

개발 로그/알고리즘

890. Find and Replace Pattern.cpp

k._. 2021. 10. 7. 19:28

map의 해당key에 값이 있는지 확인하려면 m.count("key").

true 1

else 0 리턴

class Solution {
public:
    string helper(string s){
        map<char,int>m;
        string t;
        for(int i =0; i<s.size();i++){
            if(m.count(s[i])==0) { 
                m[s[i]] = i;
                t += to_string(i);
            }
            else{
                t +=to_string(m[s[i]]);
            }
        }
        return t;
    }
    
    vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
        vector<string>res;
        
        for(int i =0; i<words.size(); i++){
            if(helper(pattern)==helper(words[i])) res.push_back(words[i]);
        }
        
        return res;
    }
};