Skip to content

Commit c5585d7

Browse files
committed
Add solution #890
1 parent d1a99ed commit c5585d7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 890. Find and Replace Pattern
3+
* https://leetcode.com/problems/find-and-replace-pattern/
4+
* Difficulty: Medium
5+
*
6+
* You have a list of words and a pattern, and you want to know which words in words matches the pattern.
7+
*
8+
* A word matches the pattern if there exists a permutation of letters p so that after replacing every
9+
* letter x in the pattern with p(x), we get the desired word.
10+
*
11+
* (Recall that a permutation of letters is a bijection from letters to letters: every letter maps to
12+
* another letter, and no two letters map to the same letter.)
13+
*
14+
* Return a list of the words in words that match the given pattern.
15+
*
16+
* You may return the answer in any order.
17+
*/
18+
19+
/**
20+
* @param {string[]} words
21+
* @param {string} pattern
22+
* @return {string[]}
23+
*/
24+
var findAndReplacePattern = function(words, pattern) {
25+
const map = (p, o = {}, count = 0) => p.split('').map(c => o[c] = o[c] || String(count++)).join('');
26+
return words.filter(word => map(word) === map(pattern));
27+
};

0 commit comments

Comments
 (0)