问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. OSCP Learning Notes - Buffer Overflows(3)

    Finding Bad Characters 1. Find the bad charaters in the following website: https://bulbsecurity.com/ ...

  2. Ethical Hacking - GAINING ACCESS(18)

    CLIENT SIDE ATTACKS Backdooring ANY file Combine backdoor with any file - Generic solution. Users ar ...

  3. JMS资源文件下载列表

    网关程序(Gateway) https://files.cnblogs.com/files/IWings/Gateway.zip 网关裁判程序(GatewayReferee) https://file ...

  4. vue的双向数据绑定实现原理(简单)

    如果有人问你,学vue学到了什么,那双向数据绑定,是必然要说的. 我们都知道,在vue中,使用数据双向绑定我们都知道是v-modle实现的. 实现原理是通过Object.defineProperty的 ...

  5. 油田问题 bfs

    #include<iostream> #include<stdio.h> #include<stdlib.h> #include<time.h> #in ...

  6. encode 和 decode 的使用

    txt = '我是字符串' txt_encode = txt.encode() print(txt) # 我是字符串 print(txt_encode) # b'\xe6\x88\x91\xe6\x9 ...

  7. PHP uksort() 函数

    ------------恢复内容开始------------ 实例 使用用户自定义的比较函数对数组 $arr 中的元素按键名进行排序: <?phpfunction my_sort($a,$b){ ...

  8. IOFFSETOF ICONTAINEROF IQUEUE_ENTRY

    #include <iostream> #define IOFFSETOF(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #de ...

  9. Rest接口加Https单向认证

    背景: 接到一个需求,客户要求某个模块的rest接口都得通过https访问,客户提供证书. 步骤: Server端证书生成 刚开始还没拿到客户的证书,所以通过jdk自带的keytools自己先生成了一 ...

  10. JavaScript动画实例:圆点的衍生

    考虑如下的曲线方程: R=S*sqrt(n) α=n*θ X=R*SIN(α) Y=R*COS(α) 其中,S和θ可指定某一个定值.对n循环取0~999共1000个值,对于每个n,按照给定的坐标方程, ...