@@ -23,3 +23,49 @@ def isIsomorphic(self, s, t):
2323
2424 return True
2525
26+ # 26.2.2024, code lại ý tưởng dùng domain-image
27+ class Solution1 :
28+ def isIsomorphic (self , s , t ):
29+ set_s = set (s )
30+ set_t = set (t )
31+
32+ map_dict = dict ()
33+
34+ for i , letter in enumerate (s ) :
35+ if letter not in map_dict :
36+ map_dict [letter ] = t [i ] # map s[i] to t[i]
37+
38+ # then create image of s using function map_dict, then compare with t
39+ image_string = ""
40+ for letter in s :
41+ image_string += map_dict [letter ]
42+
43+ # TO ENSURE THAT THERE ARE NO 2 LETTER MAP TO THE SAME IMAGE LETTER
44+ if len (map_dict .values ()) > len (set (map_dict .values ())) :
45+ return False
46+
47+ return image_string == t
48+
49+ obj = Solution1 ()
50+ s = "badc"
51+ t = "baba"
52+ print (obj .isIsomorphic (s , t ))
53+
54+
55+ class Solution2 :
56+ # IDEA: From set A and set B with the same length ==> always find a bijective function from A to B
57+
58+ def isIsomorphic (self , s , t ):
59+ set_s = set (s )
60+ set_t = set (t )
61+ zipped_set = set (zip (s , t ))
62+
63+ # when len zipped_set = len set_s, it is ensure that zipped_set is a valid function
64+ # (not exist x such that f(x) have 2 values)
65+
66+ # when len zipped_set = len set_s, it is ensure that there is no two letter map to the same letter
67+
68+ # when 3 length equals, zipped_set create a bijective function from s to t
69+ return len (set_s ) == len (set_t ) == len (zipped_set )
70+
71+
0 commit comments