leetcode day30 同构字符串

day30笔记

    • 1.题目描述
    • 2.代码构思(debug)

1.题目描述

leetcode day30 同构字符串_第1张图片

2.代码构思(debug)


leetcode day30 同构字符串_第2张图片

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        s2t, t2s = {}, {}
        for a, b in zip(s, t):
            # 对于已有映射 a -> s2t[a],若和当前字符映射 a -> b 不匹配,
            # 说明有一对多的映射关系,则返回 false ;
            # 对于映射 b -> a 也同理
            if a in s2t and s2t[a] != b or \
               b in t2s and t2s[b] != a:
                return False
                
            s2t[a], t2s[b] = b, a
            
        return True

你可能感兴趣的:(leetcode,算法,职场和发展)