Skip to main content

890. Find and Replace Pattern

https://leetcode.com/problems/find_and_replace_pattern/

Python

class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
result = []

for word in words:
if len(word) != len(pattern):
continue

mapper, mapped = dict(), set()
for i in range(len(pattern)):
p, c = pattern[i], word[i]
if p not in mapper:
if c in mapped:
break

mapper[p] = c
mapped.add(c)
else:
if mapper[p] != c:
break
else:
result.append(word)

return result