Given a list of strings words and a string pattern, return a list of words[i] that match pattern. You may return the answer in any order.

A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.

Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.

&nbsp;
Example 1:

Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -&gt; m, b -&gt; e, ...}. 
"ccc" does not match the pattern because {a -&gt; c, b -&gt; c, ...} is not a permutation, since a and b map to the same letter.


Example 2:

Input: words = ["a","b","c"], pattern = "a"
Output: ["a","b","c"]


&nbsp;
Constraints:


	1 &lt;= pattern.length &lt;= 20
	1 &lt;= words.length &lt;= 50
	words[i].length == pattern.length
	pattern and words[i] are lowercase English letters.


class Solution:
    def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
        d={}
        for i,v in enumerate(pattern):
            if v in d:
                d[v].append(i)
            else:
                d|={v:[i]}
        #DICTIONARY CONTAINING LETTERS AND THEIR INDICES
        ans=[]
        for i in words:
            e={}
            for j,v in enumerate(i):
                if v in e:
                    e[v].append(j)
                else:
                    e|={v:[j]}
        #DICTIONARY CONTAINING LETTERS OF INDICES OF CURRENT WORD
            for u,v in zip(d.values(),e.values()):
                #COMPARING EACH VALUE
                if u!=v:
                    break
            #IF SUCCESSFUL APPEND TO ANS
            else:ans.append(i)
        return ans
class Solution {
    public List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> result=new ArrayList<>();
        for(String word:words) {
            Map<Character,Character> map=new HashMap<>();
            Set<Character> set=new HashSet<>();
            int i=0;
            for(;i<word.length();i++) {
                char ch=pattern.charAt(i);
                if(map.get(ch)==null) {
                    if(set.contains(word.charAt(i))) break;
                   map.put(ch, word.charAt(i));
                    set.add(word.charAt(i));
                }
                else {
                    char mc=map.get(ch);
                    if(mc!=word.charAt(i)) break;
                }
            }
            if(i==pattern.length()) result.add(word);
        }
        return result;
    }
}
			//😉If you Like the repository don't foget to star & fork the repository😉
class Solution {
public:
    
    vector<int> found_Pattern(string pattern)
    {
	   // if string is empty return empty vector.
        if(pattern.size() == 0)
            return {};
        
        vector<int> v;
		
		// ind variable for keeping track of unique characters
        int ind = 0;
		
        unordered_map<char,int> mp;
        for(int i = 0; i<pattern.size(); ++i)
        {
			// if character not present in map, insert the char with an index,
			// increment index because for next unique character the index should be differnt.
           if(mp.find(pattern[i]) == mp.end())
           {
               mp.insert({pattern[i],ind++});
			   // also  push the index to v(numeric  pattern vector)
			   // mp[pattern[i]] gives the key to that character, here key is ind(which we are giving to every unique character)
               v.push_back(mp[pattern[i]]);
           }
            else
            {
				// if char is already in map than push index mapped to that character into the vector
                v.push_back(mp[pattern[i]]);
            }
        }
		// return numeric pattern
        return v;
    }
    vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
       
		// store numeric patten of Pattern string in v
        vector<int> v = found_Pattern(pattern);
        
        int n = words.size();
        vector<string> ans;
        
		// iterating and comparing pattern of each word with v(numeric pattern of Pattern)
        for(int i = 0; i<n; ++i)
        {
            vector<int> pattern_word = found_Pattern(words[i]);
            
			// if matched add words[i] to answer vector
            if(v == pattern_word)
                ans.push_back(words[i]);
        }
        
        return ans;
    }
};
var findAndReplacePattern = function(words, pattern) {
    var patt = patternarr(pattern)  // 010
    return words.filter(e=>patternarr(e) == patt)
};

const patternarr = function (str) {
    var result = '';
    
    for(let i=0;i<str.length;i++) {
	//finding the first index 
        result += str.indexOf(str[i])
    }
    return result
    
}
Find and Replace Pattern