Skip to content

Latest commit

 

History

History
62 lines (48 loc) · 1.51 KB

_890. Find and Replace Pattern.md

File metadata and controls

62 lines (48 loc) · 1.51 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 03, 2024

Last updated : July 03, 2024


Related Topics : Array, Hash Table, String

Acceptance Rate : 76.8 %


Solutions

Python

class Solution:
    def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
        currVal = 0
        hs = {}
        output = []
        for c in pattern :
            if c in hs :
                output.append(hs[c])
            else :
                output.append(currVal)
                hs[c] = currVal
                currVal += 1
                
        patternTuple = tuple(output)
        matches = []

        for word in words :
            if len(word) != len(pattern) :
                continue

            currVal = 0
            hs = {}
            output = []
            for c in word :
                if c in hs :
                    output.append(hs[c])
                else :
                    output.append(currVal)
                    hs[c] = currVal
                    currVal += 1
            if tuple(output) == patternTuple :
                matches.append(word)
        
        return matches