205. Isomorphic Strings - cocoder39/coco39_LC GitHub Wiki

205. Isomorphic Strings

edge case: "baba" and "badc": no two chars from s can be mapped to same char in t. each char in s can only map to one char in t. one to one mapping

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        s_to_t = {}
        t_to_s = {}
        for ch1, ch2 in zip(s, t):
            if ch1 not in s_to_t:
                s_to_t[ch1] = ch2
            elif s_to_t[ch1] != ch2:
                    return False

            if ch2 not in t_to_s:
                t_to_s[ch2] = ch1
            elif t_to_s[ch2] != ch1:
                return False 

        return True