C#LeetCode刷题之#859-亲密字符串(Buddy Strings)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3973 访问。
给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true ;否则返回 false 。
输入: A = "ab", B = "ba"
输出: true
输入: A = "ab", B = "ab"
输出: false
输入: A = "aa", B = "aa"
输出: true
输入: A = "aaaaaaabc", B = "aaaaaaacb"
输出: true
输入: A = "", B = "aa"
输出: false
提示:
- 0 <= A.length <= 20000
- 0 <= B.length <= 20000
- A 和 B 仅由小写字母构成。
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.
Input: A = "ab", B = "ba"
Output: true
Input: A = "ab", B = "ab"
Output: false
Input: A = "aa", B = "aa"
Output: true
Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true
Input: A = "", B = "aa"
Output: false
Note:
- 0 <= A.length <= 20000
- 0 <= B.length <= 20000
- A and B consist only of lowercase letters.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3973 访问。
public class Program {
public static void Main(string[] args) {
var A = "aaaaaaabc";
var B = "aaaaaaacb";
var res = BuddyStrings(A, B);
Console.WriteLine(res);
A = "ab";
B = "ab";
res = BuddyStrings2(A, B);
Console.WriteLine(res);
Console.ReadKey();
}
private static bool BuddyStrings(string A, string B) {
//暴力求解,LeetCode超时未AC
if(A.Length != B.Length) return false;
var sb = new StringBuilder(A);
//逐一测试所有字符
for(var i = 0; i < A.Length; i++) {
for(var j = i + 1; j < A.Length; j++) {
var swap = sb[i];
sb[i] = sb[j];
sb[j] = swap;
//相同时,返回 true
if(sb.ToString() == B) return true;
//重置 sb
sb = new StringBuilder(A);
}
}
//返回 false
return false;
}
private static bool BuddyStrings2(string A, string B) {
//长度不同时,直接返回 false
if(A.Length != B.Length) return false;
//用 list 统计相同位置处字符不同的索引值
var list = new List<int>();
for(var i = 0; i < A.Length; i++) {
if(A[i] != B[i]) list.Add(i);
if(list.Count > 2) break;
}
//若所有位置字符相
//那么若原字符串包含相同的字符则为亲密字符串
if(list.Count == 0) {
if(ContainsSameLetter(A)) return true;
}
//不等于 2,则没有办法通过交换获得相同结果
if(list.Count != 2) return false;
//用 sb 交换 2 个值
var sb = new StringBuilder(A);
var swap = sb[list[0]];
sb[list[0]] = sb[list[1]];
sb[list[1]] = swap;
//相同时,返回 true
return sb.ToString() == B;
}
private static bool ContainsSameLetter(string A) {
//哈希法判定是否存在相同字符
var dic = new Dictionary<char, int>();
foreach(var c in A) {
if(dic.ContainsKey(c)) {
dic[c]++;
} else {
dic[c] = 1;
}
}
//包含2个及以上的数量即存在相同字符
return dic.Where(c => c.Value >= 2).Count() >= 1;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3973 访问。
True
False
分析:
显而易见,BuddyStrings 的时间复杂度为: ,BuddyStrings2 的时间复杂度为:
。
C#LeetCode刷题之#859-亲密字符串(Buddy Strings)的更多相关文章
- LeetCode 859. 亲密字符串(Buddy Strings) 23
859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...
- C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...
- C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
- C#LeetCode刷题之#344-反转字符串(Reverse String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...
- C#LeetCode刷题之#541-反转字符串 II(Reverse String II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3951 访问. 给定一个字符串和一个整数 k,你需要对从字符串开头 ...
- C#LeetCode刷题之#443-压缩字符串(String Compression)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3943 访问. 给定一组字符,使用原地算法将其压缩. 压缩后的长度 ...
- [Swift]LeetCode859. 亲密字符串 | Buddy Strings
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
随机推荐
- 用maven打包java项目的pom文件配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- OSCP Learning Notes - Exploit(5)
Java Applet Attacks Download virtual machines from the following website: https://developer.microsof ...
- OSCP Learning Notes - Capstone(3)
DroopyCTF Walkthrough Preparation: Download the DroopyCTF virtual machine from the following website ...
- 牛客练习赛 66B题解
前言 当初思路 开始没想到异或这么多的性质,于是认为对于每个点\(u\),可以和它连边的点\(v\)的点权 \(a_v=a_u \oplus k\)(证明:\(\because\) \(a_u\opl ...
- three.js 数学方法之Plane
今天郭先生就来继续说一说three.js数学方法中的plane(平面).在三维空间中无限延伸的二维平面,平面方程用单位长度的法向量和常数表示.构造器为Plane( normal : Vector3, ...
- CentOS 下的验证码显示问题
开发环境 AND 生产环境.gif 问题: 项目部署到 CentOS 的服务器后,图片验证码请求时出现 500 错误, 日志一直是 ArrayIndexOfBoundsException:0,数组第 ...
- Linux切换用户时报错/.bash_profile: Permission denied,命令行(终端提示符)出现-bash-4.2$
Linux切换用户时报错/.bash_profile: Permission denied,命令行(终端提示符)出现-bash-4.2$ 利用su - 切换用户时,发现有一个用户切时出现如下情况 [r ...
- BUUCTF-Web Comment
dirsearch扫出/.git/目录 遂用航神写的Githacker脚本 https://github.com/wangyihang/githacker 出来的源码并不完整,使用git log ...
- 一分钟速学 | NMS, IOU 与 SoftMax
非极大抑制 NMS的英文是Non-maximum suppression的缩写. 简单的说,就是模型给出了多个重叠在一起的候选框,我们只需要保留一个就可以了.其他的重叠的候选框就删掉了,效果可见下图: ...
- IO—》字节流&字符流
字节流 一.字节输出流OutputStream OutputStream此抽象类,是表示输出字节流的所有类的超类.操作的数据都是字节,定义了输出字节流的基本共性功能方法. FileOutputStre ...