array and hashmap July 29, 2022

Find and replace pattern

Time O(n) Space O(n) Open original problem

We will create a hashmap for each word counting the length of the hashmap at that position. Then we create the same hashmap for our pattern. Then we iterate over each word, match it with the pattern and return the matched value as an array.

class Solution:
    def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
        def f(word):
            m = {}
            return [m.setdefault(c, len(m)) for c in word]

        fp = f(pattern)
        return [word for word in words if f(word) == fp]

Time Complexity: O(n)
Space Complexity: O(n)