把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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2022-12-23
  • 2021-11-17
  • 2021-12-23
  • 2021-06-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-12
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
  • 2022-12-23
相关资源
相似解决方案