#890

Find and Replace Pattern

pupil · 470 · lc medium +26 · verified · 77% accepted · 4,026 likes · top 88%

Description

Given a list of strings words and a string pattern, identify which words match the pattern and return them in any order.

A word matches if a one-to-one letter substitution (bijection) can transform the pattern into that word — every distinct letter in the pattern maps to a unique distinct letter in the word.

Example 1:

Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, since a and b map to the same letter.

Example 2:

Input: words = ["a","b","c"], pattern = "a"
Output: ["a","b","c"]

Code

1
2
3