Simple python solution, no fancy

  • 1
    def isIsomorphic(self, s, t):
        s_dict, s_list, t_list = dict(), list(s), list(t)
        if len(s_list) != len(t_list):
            return False
        if len(set(s_list)) != len(set(t_list)):
            return False
        for i in range(len(s_list)):
            s_val, t_val = s_list[i], t_list[i]
            if s_val in s_dict and s_dict[s_val] != t_val:
                return False
                s_dict[s_val] = t_val
        return True

Log in to reply

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