-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsomorphicStrings
More file actions
27 lines (21 loc) · 904 Bytes
/
IsomorphicStrings
File metadata and controls
27 lines (21 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#Given two strings s and t, determine if they are isomorphic.
#Two strings s and t are isomorphic if the characters in s can be replaced to get t.
#All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character,
#but a character may map to itself.
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
s_to_t = {}
t_to_s = {}
for x, i in enumerate(s):
if i in s_to_t:
if s_to_t.get(i) != t[x]:
return False
else:
s_to_t.update({i:t[x]})
if t[x] in t_to_s.values():
if t_to_s.get(i) != t[x]:
return False
else:
t_to_s.update({i:t[x]})
if s_to_t == t_to_s:
return True