问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. LeetCode 859. 亲密字符串(Buddy Strings) 23

    859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...

  2. C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...

  3. C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...

  4. C#LeetCode刷题之#345-反转字符串中的元音字母​​​​​​​(Reverse Vowels of a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...

  5. C#LeetCode刷题之#344-反转字符串​​​​​​​(Reverse String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...

  6. C#LeetCode刷题之#541-反转字符串 II(Reverse String II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3951 访问. 给定一个字符串和一个整数 k,你需要对从字符串开头 ...

  7. C#LeetCode刷题之#443-压缩字符串​​​​​​​(String Compression)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3943 访问. 给定一组字符,使用原地算法将其压缩. 压缩后的长度 ...

  8. [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 ...

  9. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

随机推荐

  1. mysql groupby 字段合并问题(group_concat)

    在我们的日常mysql查询中,我们可能会遇到这样的情况: 对表中的所有记录进行分类,并且我需要得到每个分类中某个字段的全部成员. 上面的话,大家看起来可能不太好懂,下面举一个例子来给大家说明. 现在我 ...

  2. 牛客练习赛66A题解

    思路 令 \(sq=\sqrt x\) ,则答案必然在 $ sq^2$ 和 $ (sq+1)^2 $ 之间,两者比较一下谁离 \(x\) 最近,就是答案了. 代码 #include <bits/ ...

  3. TestNg失败重跑—解决使用 dataProvider 参数化用例次数冲突问题

    问题背景 在使用 testng 执行 UI 自动化用例时,由于 UI自动化的不稳定性,我们在测试的时候,往往会加上失败重跑机制.在不使用 @DataProvider 提供用例参数化时,是不会有什么问题 ...

  4. Flask前后端分离项目案例

    简介 学习慕课课程,Flask前后端分离API后台接口的实现demo,前端可以接入小程序,暂时已经完成后台API基础架构,使用postman调试. git 重构部分: token校验模块 auths认 ...

  5. Python基础教程(第3版)PDF高清完整版免费下载|百度云盘

    百度云盘:Python基础教程(第3版)PDF高清完整版免费下载 提取码:gkiy 内容简介 本书包括Python程序设计的方方面面:首先从Python的安装开始,随后介绍了Python的基础知识和基 ...

  6. 基于Bilateral Attention和Pyramid Filling Block的图像修复方法

    One-Stage Inpainting with Bilateral Attention and Pyramid Filling Block 论文链接:https://arxiv.org/abs/1 ...

  7. https://blog.csdn.net/yongchaocsdn/article/details/53355296

    https://blog.csdn.net/yongchaocsdn/article/details/53355296

  8. PHP操作Redis步骤详解

    一.Redis连接与认证 $redis = new Redis(); //连接参数:ip.端口.连接超时时间,连接成功返回true,否则返回false $ret = $redis->connec ...

  9. Caused by: java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'c.id'

    打开mysql客户端,输入 select @@global.sql_mode 再执行 set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DA ...

  10. PHP sha1_file() 函数

    实例 计算文本文件 "test.txt" 的 SHA-1 散列: <?php高佣联盟 www.cgewang.com$filename = "test.txt&qu ...