Skip to content

Commit

Permalink
Create find-and-replace-pattern.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Aug 19, 2018
1 parent 763aef1 commit 355e67c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Python/find-and-replace-pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Time: O(n * l)
# Space: O(l)

import itertools


class Solution(object):
def findAndReplacePattern(self, words, pattern):
"""
:type words: List[str]
:type pattern: str
:rtype: List[str]
"""
def match(word):
lookup = {}
for x, y in itertools.izip(pattern, word):
if lookup.setdefault(x, y) != y:
return False
return len(set(lookup.values())) == len(lookup.values())

return filter(match, words)

0 comments on commit 355e67c

Please sign in to comment.