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.
分析:
- 先判断是不是组成相同
- 在判断不同的个数是2还是0
- 如果是0,判断A中有没有相同字符
思路:
如果仔细写应该能写优美一点把,但是瑞了,不想写了。
1 | class Solution(object): |