leetcode 859 亲密字符串 Buddy Strings python 多种解法,最简代码

所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
'''
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B.



Example 1:

Input: A = "ab", B = "ba"
Output: true
Example 2:

Input: A = "ab", B = "ab"
Output: false
Example 3:

Input: A = "aa", B = "aa"
Output: true
Example 4:

Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true
Example 5:

Input: A = "", B = "aa"
Output: false


Note:

0 <= A.length <= 20000
0 <= B.length <= 20000
A and B consist only of lowercase letters.


'''



class Solution:
    def buddyStrings(self, A: str, B: str) -> bool:

        # 三种情况
        # 1、 两个字符串长度不相等,必然错误
        # 2、 两个字符串完全一致,需要至少有一个字母存在超过两次
        # 3、 两个字符串恰好只有两个位置不一样(只发生了一次交换)

        # Approach one
        # if len(A) != len(B) : return False
        # if A == B:
        #     for i in range(97,123,1):
        #         if A.count(chr(i)) >= 2 :
        #             return True
        #     return False
        # else:
        #     res = {}
        #     for i,n in enumerate(A):
        #         if len(res.keys()) > 1 :return False
        #         if n != B[i]:
        #             if n not in res.values():
        #                 res[n] = B[i]
        #             if B[i] in res.keys() and n in res.values():
        #                 res[B[i]] = True
        #     for i in res.values():
        #         return  True == i


        # Approach two
        # if len(A) != len(B) : return False
        # if A == B:
        #     for i in range(97,123,1):
        #         if A.count(chr(i)) >= 2 :
        #             return True
        #     return False
        # else:
        #     res = [[i,j] for i,j in zip(A,B) if i != j]
        #     if len(res) != 2: return False
        #     else:  return res[0][::-1] == res[1]



        # Approach three
        if len(A) != len(B) : return False
        if A == B: return len(set(A)) < len(B)
        else:
            res = [[i,j] for i,j in zip(A,B) if i != j]
            return res[0][::-1] == res[1] and len(res) == 2

所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值