-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0890-find-and-replace-pattern.js
More file actions
27 lines (26 loc) · 991 Bytes
/
0890-find-and-replace-pattern.js
File metadata and controls
27 lines (26 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* 890. Find and Replace Pattern
* https://leetcode.com/problems/find-and-replace-pattern/
* Difficulty: Medium
*
* 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.
*/
/**
* @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));
};