Leetcode 926 Solution

This article provides solution to leetcode question 926 (find-and-replace-pattern)

https://leetcode.com/problems/find-and-replace-pattern

Solution

class Solution: def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]: def match(word, pattern): if len(word) != len(pattern): return False
m = {} s = set() for w, p in zip(word, pattern): if p not in m and w in s: return False
if p in m and m[p] != w: return False
m[p] = w s.add(w) return True
ans = [] for word in words: if match(word, pattern): ans.append(word) return ans