Python Solution - beats 90%


  • 2
    A
    class Solution(object):
        def isIsomorphic(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """
            if not s and not t:
                return True
            elif s and t:
                if len(s) != len(t):
                    return False
                elif len(set(s)) != len(set(t)):
                    return False
                else:
                    mapping = {}
                    for ind in range(len(s)):
                        if s[ind] in mapping:
                            if mapping[s[ind]] != t[ind]:
                                return False
                        else:
                            mapping[s[ind]] = t[ind]
                return True
            else:
                return False
            
    

Log in to reply
 

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