Created
May 21, 2021 07:41
-
-
Save vrat28/05c603514e1d02f3484ccb4461b08809 to your computer and use it in GitHub Desktop.
Find and Replace (Python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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