Isomorphic Strings (Leetcode 205)

Problem Link: https://leetcode.com/problems/isomorphic-strings/

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        d1={}
        n=len(s)
        for i in range(n):
            char1=s[i]
            char2=t[i]
            if(char1 in d1):
                if(d1[char1]!=char2):
                    return False
            else:
                d1[char1]=char2
        
        d2={}
        for i in range(n):
            char1=s[i]
            char2=t[i]
            if(char2 in d2):
                if(d2[char2]!=char1):
                    return False
            else:
                d2[char2]=char1
        
        return True     

Last updated