Leetcode 890. Find and Replace Pattern

把pattern映射到数字,也就是把pattern标准化.

比如abb和cdd如果都能标准化为011,那么就是同构的.

class Solution:
    def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
        p = self.get_pattern(pattern)
        ans = []
        for w in words:
            if self.get_pattern(w) == p:
                ans.append(w)
        return ans

    def get_pattern(self, word: str) -> List[int]:
        t = 0
        dic = {word[0]: t}
        ans = []
        for v in word[1:]:
            if v in dic.keys():
                ans.append(dic[v])
            else:
                t += 1
                dic[v] = t
                ans.append(dic[v])
        return ans
原文地址:https://www.cnblogs.com/zywscq/p/10681022.html