57 ms Python solution


  • 1
    R
    def isIsomorphic(self, s, t):
            if s is None and t is None:
                return True
            if s == t:
                return True
            mp = {} # Dictionary to maintain mapping between the characters in both strings
            for i in range(len(s)):
                if s[i] in mp:
                    if mp[s[i]] != t[i]: 
                        return False
                else:
                    if t[i] in t[0:i]:
                        return False
                    mp[s[i]] = t[i]
            return True

  • 0
    L

    if t[i] in t[0:i]:

    I guess most people will confuse on this line.

    This Line means: check the t[i] whether has been maped to other chacter in s[0:i].


  • 0
    L

    nice solution! btw, the first None check is not necessary
    if s is None and t is None:
    return True


Log in to reply
 

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