Notes

Personal notes on various topics

View on GitHub

Intuition

To check if two strings are isomorphic, we need to ensure that each character in the first string maps to a unique character in the second string, and that the mapping is consistent throughout both strings.

Approach

Complexity

Code

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        if len(s) != len(t):
            return False

        map_dict = {}
        mapped_set = set()

        for s_c, t_c in zip(s, t):
            map_dict[s_c] = map_dict.get(s_c, t_c)
            if map_dict[s_c] != t_c:
                return False

            mapped_set.add(map_dict[s_c])

        if len(map_dict) != len(mapped_set):
            return False

        return True

Back to Problem Statement