这题跟word pattern有点像,不过要是用word pattern的替换法来做的话肯定会超时的,这题的替换仅仅是单个字符对单个字符。所以用别的方法来看。代码如下:
class Solution(object):
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
dict1 = {}
if len(s) == 0:
if len(t) == 0:
return True
else:
return False
else:
if len(t) == 0:
return False
if len(s) != len(t):
return False
for i in range(len(s)):
if s[i] not in dict1.keys():
dict1[s[i]] = t[i]
else:
if t[i] != dict1[s[i]]:
return False
else:
set1 = set(dict1.values())
if len(set1) == len(dict1.values()):
return True
else:
return False