A different but instinct and more general solution (Python)


  • 0

    from sets import Set
    class Solution(object):
    def isIsomorphic(self, s, t):
    """
    :type s: str
    :type t: str
    :rtype: bool
    """

        # convert the string (list domain) into a char map (dict domain) that records the indices of each character
        sDict={}
        for i,x in enumerate(s):
            if x in sDict:
                sDict[x].append(i)
            else:
                sDict[x]=[i]
        tDict={}
        for i,x in enumerate(t):
            if x in tDict:
                tDict[x].append(i)
            else:
                tDict[x]=[i]>! Spoiler
        
        # convert the char map into a char set (therefore the order will not be considered) for comparison
        sSet=Set([tuple(x[1]) for x in sDict.items()])
        tSet=Set([tuple(x[1]) for x in tDict.items()])
            
        # compare the two set
        return sSet==tSet

Log in to reply
 

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