Simple python solution > 95%

  • 1
    class Solution(object):
        def isIsomorphic(self, s, t):
            :type s: str
            :type t: str
            :rtype: bool
            map = {}
            used = set()
            for i in xrange(len(s)):
                if s[i] not in map:
                    if t[i] in used:
                        return False
                        map[s[i]] = t[i]
                    if map[s[i]] != t[i]:
                        return False
            return True

    The idea here is to keep a dictionary to map characters and a set to keep track of which characters have already been mapped to, i.e. if 'a' maps to 'a', 'b' cannot map to 'a'.

Log in to reply

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