Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 879 Bytes

890.md

File metadata and controls

42 lines (31 loc) · 879 Bytes

Find and Replace Pattern

Description

link


Solution

  • See Code

Code

Complexity T : O(n) M : O(n)

class Solution:
    def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
        res = []
    
        def match(word, pattern):
            if len(word) != len(pattern):
                return False
            d = {}
            for w, p in zip(word, pattern):
                if w not in d:
                    if p in d.values():
                        return False
                    d[w] = p
                else:
                    if d[w] != p:
                        return False
            return True
                    
        for w in words:
            if match(w, pattern):
                res.append(w)
        return res