Peeush Agarwal > Engineer. Learner. Builder.

I am a Machine Learning Engineer passionate about creating practical AI solutions using Machine Learning, NLP, Computer Vision, and Azure technologies. This space is where I document my projects, experiments, and insights as I grow in the world of data science.

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