Skip to main content

205. Isomorphic Strings

https://leetcode.com/problems/isomorphic-strings/

Python

from collections import defaultdict


class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False

mapper_s, mapper_t = defaultdict(str), defaultdict(str)

for i in range(len(s)):
if s[i] not in mapper_s and t[i] not in mapper_t:
mapper_s[s[i]] = t[i]
mapper_t[t[i]] = s[i]
continue

if mapper_s[s[i]] != t[i] or mapper_t[t[i]] != s[i]:
return False

return True