-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#0205 Isomorphic Strings.py
More file actions
32 lines (26 loc) · 967 Bytes
/
Copy path#0205 Isomorphic Strings.py
File metadata and controls
32 lines (26 loc) · 967 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
28
29
30
31
32
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
"""
Determines if two strings s and t are isomorphic.
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
No two characters may map to the same character, but a character may map to itself.
Args:
s (str): The first string.
t (str): The second string.
Returns:
bool: True if the strings are isomorphic, False otherwise.
"""
if len(s) != len(t):
return False
char_map = {}
mapped_chars = set()
for char_s, char_t in zip(s, t):
if char_s in char_map:
if char_map[char_s] != char_t:
return False
else:
if char_t in mapped_chars:
return False
char_map[char_s] = char_t
mapped_chars.add(char_t)
return True