Isomorphic Strings LeetCode solution in Python

1
class Solution:
2
def isIsomorphic(self, s: str, t: str) -> bool:
3
dict1 = {}
4
m = ""
5
# creating a dictionary by mapping each element from string S to string T
6
for i, j in zip(s, t):
7
# this for the cases like "badc" and "baba" so we dont want two keys mapping to same value hence we can reject directly
8
if j in dict1.values() and i not in dict1.keys():
9
return False
10
dict1[i] = j
11

12
# now take each letter from string s and using dictionary values replace it with that specific character
13
for k in s:
14
m = m + dict1[k]
15

16
# now if newly formed string m == T is same then the strings are Isomorphic
17
if m == t:
18
return True
19
else:
20
return False

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0