We maintain two hash maps for bi-directional mapping between characters of s and t. Alternatively, we can use a single map and a set to ensure no two characters map to the same target.
- If
len(s) != len(t), return False. - Initialize
mapping_s_t = {},mapped_t_chars = set(). - For each pair of characters
(c1, c2)inzip(s, t):- If
c1inmapping_s_t:- If
mapping_s_t[c1] != c2, return False.
- If
- Else:
- If
c2inmapped_t_chars, return False. mapping_s_t[c1] = c2,mapped_t_chars.add(c2).
- If
- If
- Return True.
- Time Complexity: O(N).
- Space Complexity: O(1) (size of character set).
def is_isomorphic(s, t):
if len(s) != len(t): return False
map_s = {}
map_t = {}
for c1, c2 in zip(s, t):
if c1 in map_s:
if map_s[c1] != c2: return False
elif c2 in map_t:
return False
else:
map_s[c1] = c2
map_t[c2] = c1
return True