LeetCode - Find and Replace Pattern(890)

marafo·2021년 8월 7일

String - Medium

class Solution:
    def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
        
        answer = []
        
        # A length of pattern = A length of words[i]
        for i in range(len(words)):
            dic = {}
            change = ''
            check = []
            for j in range(len(words[i])):
                letter = words[i][j]
                if (letter not in dic) and (check.count(pattern[j]) == 0):
                    dic[letter] = pattern[j]
                    check.append(pattern[j])
                    change += pattern[j]
                elif letter not in dic and check.count(pattern[j]) != 0:
                    change += letter
                elif letter in dic:
                    change += dic[letter]
            
            if change == pattern:
                answer.append(words[i])
          
        return answer
profile
프론트 개발자 준비

0개의 댓글