Find and Replace Pattern LeetCode solution in JavaScript

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

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0