Back to all solutions
#890 - Find and Replace Pattern
Problem Description
You have a list of words and a pattern, and you want to know which words in words matches the pattern.
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.) Return a list of the words in words that match the given pattern.
You may return the answer in any order.
Solution
/**
* @param {string[]} words
* @param {string} pattern
* @return {string[]}
*/
var findAndReplacePattern = function(words, pattern) {
const map = (p, o = {}, count = 0) => p.split('').map(c => o[c] = o[c] || String(count++)).join('');
return words.filter(word => map(word) === map(pattern));
};