【LeetCode】859. Buddy Strings 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: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.
题目大意
当且仅当交换两个字符串中的两个字符的时候,看两个字符串能否完全相等。
解题方法
字典
分析如下:
- 如果两个字符串长度不等,那么一定不满足条件
- 如果两个字符串完全相等,如果其中存在至少两个相等字符,那么满足条件
- 如果两个字符串长度相等且只有两个位置的字符不等,记录下这两个位置,如果这两个字符串的该两个位置字符是恰好错位的,那么满足条件。
代码如下:
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]]
C++版本如下:
class Solution {
public:
bool buddyStrings(string A, string B) {
if (A.size() != B.size()) return false;
vector<int> ca(26);
vector<int> cb(26);
int N = A.size();
int diff = 0;
for (int i = 0; i < N; i++) {
if (A[i] != B[i] && diff++ > 2) return false;
ca[A[i] - 'a']++;
cb[B[i] - 'a']++;
}
for(int i = 0; i < 26; i++) {
if (diff == 0 && ca[i] > 1) return true;
if (ca[i] != cb[i]) return false;
}
return diff == 2;
}
};
参考资料:https://zxi.mytechroad.com/blog/string/leetcode-859-buddy-strings/
日期
2018 年 7 月 4 日 —— 夏天挺热的,记得吃饭,防止低血糖
2018 年 11 月 30 日 —— 又到了周末
【LeetCode】859. Buddy Strings 解题报告(Python & C++)的更多相关文章
- leetcode 859. Buddy Strings
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 859. Buddy Strings - LeetCode
Question 859. Buddy Strings Solution 题目大意: 两个字符串,其中一个字符串任意两个字符互换后与另一个字符串相等,只能互换一次 思路: diff 记录不同字符数 两 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【Leetcode_easy】859. Buddy Strings
problem 859. Buddy Strings solution: class Solution { public: bool buddyStrings(string A, string B) ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
随机推荐
- Linux—find在指定路径下查找文件或目录
find /指定路径 -name "*filename*" find /指定路径 -name "*filename*" 2>/dev/null ...
- A Child's History of England.13
Then came the boy-king, Edgar, called the Peaceful, fifteen years old. Dunstan, being still the real ...
- HDFS【概述、数据流】
目录 概述 定义 优缺点 HDFS组成架构 HDFS文件块大小 HDFS数据流 写数据 读数据 网络拓扑-节点距离计算 机架感知(写数据的副本存储节点选择) 概述 定义 HDFS是一个分布式文件管理系 ...
- CAS你知道吗
1.比较并交换 CASDemo /** * CAS => compareAndSet * 比较并交换 */ public class CASDemo { public static void m ...
- Docker学习(二)——Docker容器使用
Docker容器使用 1.Docker客户端 命令docker可以查看到Docker客户端的所有命令选项. 命令docker command --help更深入的了解指定的Do ...
- Android Loader异步装载
一.Loader简介: (一).Loader的概念: 装载器从android3.0开始引进.它使得在activity或fragment中异步加载数据变得简单. 当成批显示数据的时候,为了使用户体验更好 ...
- Android Bitmap 全面解析(一)加载大尺寸图片
压缩原因:1.imageview大小如果是200*300那么加载个2000*3000的图片到内存中显然是浪费可耻滴行为;2.最重要的是图片过大时直接加载原图会造成OOM异常(out of memory ...
- Undefined symbols for architecture arm64:问题
Undefined symbols for architecture arm64: "_sqlite3_prepare_v2", referenced from: +[HMJSch ...
- clickhouse客户端使用
测试初始化 clickhouse-client -m create database if not exists test; use test; drop table test; create tab ...
- pageBean的实体类
package com.hopetesting.domain;import java.util.List;/** * @author newcityman * @date 2019/9/7 - 19: ...