Skip to content
LC-0890 Medium LeetCode

890. Find and Replace Pattern

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 77% Topics: Array, Hash Table, String
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n * l)
# Space: O(1)

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)

Solution from kamyu104/LeetCode-Solutions · MIT