Back to Problem
Official 2205 views

Editorial: Isomorphic Strings

by admin ·

35

1 Intuition

The mapping must be a bijection.

2 Approach

Keep forward and backward maps; a conflict in either direction means the strings are not isomorphic.

3 Complexity Analysis

Time O(n), Space O(1) for a fixed alphabet.

4 Solution Code

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        fwd, back = {}, {}
        for a, b in zip(s, t):
            if fwd.setdefault(a, b) != b or back.setdefault(b, a) != a:
                return False
        return True

5 Code Walkthrough

The reference solution below implements the approach step by step.