diff --git a/Python/find-and-replace-pattern.py b/Python/find-and-replace-pattern.py new file mode 100644 index 000000000..884e8e08b --- /dev/null +++ b/Python/find-and-replace-pattern.py @@ -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)