- See Code
Complexity T : O(n) M : O(n)
class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
res = []
def match(word, pattern):
if len(word) != len(pattern):
return False
d = {}
for w, p in zip(word, pattern):
if w not in d:
if p in d.values():
return False
d[w] = p
else:
if d[w] != p:
return False
return True
for w in words:
if match(w, pattern):
res.append(w)
return res