Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0205 - Isomorphic Strings
    0205 - Isomorphic Strings
    0205 - Isomorphic Strings
    0205 - Isomorphic Strings

    0205 - Isomorphic Strings

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/isomorphic-strings/

    Solution 1 - Hash Map (Two Dictionaries)

    Use two dictionaries to track character mappings from s → t and t → s. For each character pair, check if the mapping is consistent. If any conflict is found, return False.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Index Mapping

    Record the last seen index for each character in both strings. If at any position the recorded indices differ, the strings are not isomorphic.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0205.1 - Solution 1 - Hash Map (Two Dictionaries) """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def isIsomorphic(self, s: str, t: str) -> bool:
            """Isomorphic Strings Function"""
            map_s_to_t = {}
            map_t_to_s = {}
    
            for char_s, char_t in zip(s, t):
                if char_s in map_s_to_t:
                    if map_s_to_t[char_s] != char_t:
                        return False
                else:
                    map_s_to_t[char_s] = char_t
    
                if char_t in map_t_to_s:
                    if map_t_to_s[char_t] != char_s:
                        return False
                else:
                    map_t_to_s[char_t] = char_s
    
            return True
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().isIsomorphic("egg", "add"))       # True
        print(Solution().isIsomorphic("foo", "bar"))       # False
        print(Solution().isIsomorphic("paper", "title"))   # True
        print(Solution().isIsomorphic("badc", "baba"))     # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0205.2 - Solution 2 - Index Mapping """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def isIsomorphic(self, s: str, t: str) -> bool:
            """Isomorphic Strings Function"""
            index_s = {}
            index_t = {}
    
            for i, (char_s, char_t) in enumerate(zip(s, t)):
                if index_s.get(char_s) != index_t.get(char_t):
                    return False
                index_s[char_s] = i + 1
                index_t[char_t] = i + 1
    
            return True
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().isIsomorphic("egg", "add"))       # True
        print(Solution().isIsomorphic("foo", "bar"))       # False
        print(Solution().isIsomorphic("paper", "title"))   # True
        print(Solution().isIsomorphic("badc", "baba"))     # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()