Find and Replace Pattern LeetCode solution in JavaScript
1var findAndReplacePattern = function (words, pattern) {2let ans = [],3codex = new Map();4const translate = (char) => {5if (!codex.has(char)) codex.set(char, String.fromCharCode(97 + codex.size));6return codex.get(char);7};8const compare = (word) => {9codex.clear();10for (let i = 0; i < word.length; i++)11if (translate(word[i]) !== cipher[i]) return;12ans.push(word);13};14let cipher = new Array(pattern.length);15for (let i = 0; i < pattern.length; i++)16cipher[i] = translate(pattern.charAt(i));17words.forEach(compare);18return ans;19};