Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find and Replace (Python)
class Solution(object):
def findAndReplacePattern(self, words, pattern):
def match(word):
m1, m2 = {}, {}
for w, p in zip(word, pattern):
if w not in m1: m1[w] = p
if p not in m2: m2[p] = w
if (m1[w], m2[p]) != (p, w):
return False
return True
return filter(match, words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment