python easy ac solution map s->t and t->s


  • 0
    H
    class Solution(object):
        def isIsomorphic(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """
            # map the s -> t and t -> s, make sure it has one to one mapping realationship
            if len(s) != len(t):
                return False
            charMap1 = {}
            charMap2 = {}
            for i in range(len(s)):
                if s[i] not in charMap1 and t[i] not in charMap2:
                    charMap1[s[i]] = t[i]
                    charMap2[t[i]] = s[i]
                elif s[i] in charMap1 and t[i] in charMap2:
                    if charMap1[s[i]] != t[i] or charMap2[t[i]] != s[i]:
                        return False
                else:
                    return False
            return True
        
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.