Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created May 21, 2021 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/05c603514e1d02f3484ccb4461b08809 to your computer and use it in GitHub Desktop.
Save vrat28/05c603514e1d02f3484ccb4461b08809 to your computer and use it in GitHub Desktop.
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