class Solution {
public:
    vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
        vector<string>res;
        int n = pattern.size();
        for (string &word : words) {
            if (word.size() != n)continue;
            vector<char>pletter(256, '#');
            vector<char>wletter(256, '#');
            bool flag = false;
            //进行映射
            for (int i = 0; i < n; i++) {
                if (pletter[pattern[i]] == '#'&&wletter[word[i]]=='#') {
                    pletter[pattern[i]] = word[i];
                    wletter[word[i]] = pattern[i];
                }
                else if (pletter[pattern[i]] == word[i]&& wletter[word[i]]==pattern[i]) {
                    continue;
                }
                else {
                    flag = true;
                    break;
                }   
            }
            if (!flag)res.push_back(word);
        }
        return res;
    }
};
