Subject address (205. Isomorphic string)

Leetcode-cn.com/problems/is…

Topic describes

Given two strings s and t, determine whether they are isomorphic. If the characters in S can be replaced by some mapping to give t, then the two strings are isomorphic. Each occurrence of a character should map to another character without changing the order of the characters. Different characters cannot be mapped to the same character. The same character can only be mapped to the same character. Characters can be mapped to themselves. Example 1: Input: s = "egg", t = "add" Output: true Example 2: Input: s = "foo", t = "bar" Output: false Example 3: Input: s = "paper", t = "title" Output: true Hint: We can assume that s and t have the same length.Copy the code

Train of thought

Use the dictionary to see if there’s a match

code

  • Language support: Python3

Python3 Code:


class Solution:
    def isIsomorphic(self, s: str, t: str) - >bool:
        resDict = dict(a)for index,val in enumerate(s):
            if val in resDict:
                ifresDict[val] ! = t[index]:return False
            else:
                resDict[val] = t[index]
        resDict = dict(a)for index,val in enumerate(t):
            if val in resDict:
                ifresDict[val] ! = s[index]:return False
            else:
                resDict[val] = s[index]
        return True

Copy the code

Complexity analysis

Let n be the length of the array.

  • Time complexity: O(n)O(n)O(n)
  • Space complexity: O(n)O(n)O(n)