890. Find and Replace Pattern
Question
Given a list of strings
words
and a stringpattern
, return a list ofwords[i]
that matchpattern
. You may return the answer in any order.A word matches the pattern if there exists a permutation of letters
p
so that after replacing every letterx
in the pattern withp(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.
Solution
遍历words,对pattern和words中的字符建立映射。
used[]数组记录访问状况。
如未建立映射关系,且word中的字符已建立映射,则不在结果中添加当前字符串。
如未建立映射,则建立新的word与pattern的映射关系。
如果当前已经建立映射,但是当前字符违反了映射关系,则不在结果中添加当前字符串。
Code
1 | class Solution { |
890. Find and Replace Pattern
https://xuanhe95.github.io/2022/07/29/890-Find-and-Replace-Pattern/