Find and Replace Pattern(C++查詢和替換模式)

coolsunxu發表於2021-01-03
class Solution {
public:
    bool compare(string &s,string &pattern,unordered_map<char,vector<int>> &mp) {
        unordered_map<char,vector<int>> mw;
        for(int i=0;i<s.length();i++) {
            mw[s[i]].push_back(i);
        }
        
        for(int i=0;i<s.length();i++) {
            if(mw[s[i]]!=mp[pattern[i]]) return false;
        }
        return true;
        
    }
    vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
        unordered_map<char,vector<int>> mp;
        for(int i=0;i<pattern.length();i++) {
            mp[pattern[i]].push_back(i);
        }
        
        vector<string> v;
        for(int i=0;i<words.size();i++) {
            if(compare(words[i],pattern,mp)) v.push_back(words[i]);
        }
        return v;
    }
};

 

相關文章