【LeetCode】859. Buddy Strings 解题报告(Python)

【LeetCode】859. Buddy Strings 解题报告(Python)

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/buddy-strings/description/

题目描述:

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.

题目大意

当且仅当交换两个字符串中的两个字符的时候,看两个字符串能否完全相等。

解题方法

分析如下:

  1. 如果两个字符串长度不等,那么一定不满足条件
  2. 如果两个字符串完全相等,如果其中存在至少两个相等字符,那么满足条件
  3. 如果两个字符串长度相等且只有两个位置的字符不等,记录下这两个位置,如果这两个字符串的该两个位置字符是恰好错位的,那么满足条件。

代码如下:

class Solution:
    def buddyStrings(self, A, B):
        """
        :type A: str
        :type B: str
        :rtype: bool
        """
        if len(A) != len(B):
            return False
        diff = 0
        idxs = []
        for i, a in enumerate(A):
            if B[i] != a:
                diff += 1
                idxs.append(i)
        counter = dict()
        if diff == 0:
            for a in A:
                if a in counter and counter[a]:
                    return True
                else:
                    counter[a] = True
        if diff != 2:
            return False
        return A[idxs[0]] == B[idxs[1]] and A[idxs[1]] == B[idxs[0]]

日期

2018 年 7 月 4 日 ———— 夏天挺热的,记得吃饭,防止低血糖

猜你喜欢

转载自blog.csdn.net/fuxuemingzhu/article/details/80916203