LeetCode: Find And Replace Pattern Solution

1/**
2 * @param {string[]} words
3 * @param {string} pattern
4 * @return {string[]}
5 */
6var findAndReplacePattern = function (words, pattern) {
7 return words.filter(word => {
8 const map = new Map()
9 const set = new Set()
10 return word.split("").every((char, i) => {
11 if (!set.has(char) && !map.has(pattern[i])) {
12 map.set(pattern[i], char)
13 set.add(char) // avoid two diff pattern[i] match for one same char
14 }
15 return map.get(pattern[i]) === char
16 })
17 })
18}

Comments

Loading comments...

Tags

leetcode

string

hash table

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: N Queens

HoningJS

Search Posts