【发布时间】:2023-02-18 09:35:11
【问题描述】:
我的代码没有通过这个测试用例。有人可以帮我理解我的代码有什么问题吗?
输入: “坏” “爸爸” 输出: 真的 预期的: 错误的
class Solution(object):
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
dict = {}
if len(s)==0 or len(t)==0:
return False
for i in range(len(s)):
if s[i] in dict:
if dict[s[i]] != t[i]:
return False
else:
dict[s[i]] = t[i]
return True
【问题讨论】:
-
在您的 else 子句中,您需要检查
t[i]是否已作为映射字典中的值存在(因为不允许两个不同的字符映射到相同的字符)
标签: python dictionary