205. 同构字符串
给定两个字符串
s 和 t ,判断它们是否是同构的。如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。
每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。
class Solution: def isIsomorphic(self, s: str, t: str) -> bool: d1 = {} d2 = {} l1 = len(s) l2 = len(t) if l1!=l2: return False for i in range(l1): a = s[i] b = t[i] if a not in d1 and b not in d2: d1[a]=b d2[b]=a else: if a in d1 and d1[a]!=b: return False if b in d2 and d2[b]!=a: return False return True

浙公网安备 33010602011771号