【问题标题】:Leetcode 205: Isomorphic StringsLeetcode 205:同构字符串
【发布时间】: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


【解决方案1】:

如问题描述中所述:

没有两个字符可以映射到同一个字符

对于输入s = badct = babas中的字母bd都映射到t中的字母b,这违反了规则,应该返回false。

正如 bui 所提到的,我们可以在 else 子句中添加一个条件,以确保字典中的值是唯一的:

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:
                if t[i] in dict.values():  
                    return False
                dict[s[i]] = t[i]
        return True

【讨论】:

    【解决方案2】:

    您只检查了 1 个字符串,即 s 是否与 t 同构? 您还需要检查t,即t是否同构于s

    class Solution(object):
        def isIsomorphic(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """
            dict = {}
            dict2 = {}
            
            if len(s)==0 or len(t)==0:
                return False
            if len(s)!= len(t):
                return False
            for a,b in zip(s,t):
                if a not in dict:
                    dict[a]=b
                else:
                    if dict[a]!=b:
                        return False
                if b not in dict2:
                    dict2[b] = a
                else:
                    if dict2[b]!=a:
                        return False
            return True
    

    【讨论】:

      【解决方案3】:

      这是更pythonic的解决方案

      class Solution(object):
          def isIsomorphic(self, s, t):
              """
              :type s: str
              :type t: str
              :rtype: bool
              """
              return [s.index(ch) for ch in s] == [t.index(ch) for ch in t]
      

      等效解

      class Solution(object):
          def isIsomorphic(self, s, t):
              """
              :type s: str
              :type t: str
              :rtype: bool
              """
              return [*map(s.index, s)] == [*map(t.index, t)]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-07
        • 1970-01-01
        • 2015-09-14
        • 1970-01-01
        • 2022-09-23
        • 2021-10-10
        • 1970-01-01
        • 2021-03-30
        相关资源
        最近更新 更多