Dashboard Temp Share Shortlinks Frames API

HTMLify

Isomorphic Strings
Views: 4 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
    def areIsomorphic(self, s1, s2):
        if len(s1) != len(s2):
            return False
        
        map_s1_to_s2 = {}
        map_s2_to_s1 = {}
        
        for char1, char2 in zip(s1, s2):
            if char1 in map_s1_to_s2:
                if map_s1_to_s2[char1] != char2:
                    return False
            else:
                map_s1_to_s2[char1] = char2
                
            if char2 in map_s2_to_s1:
                if map_s2_to_s1[char2] != char1:
                    return False
            else:
                map_s2_to_s1[char2] = char1
                
        return True