给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true ;否则返回 false 。

示例 1:

输入: A = "ab", B = "ba"
输出: true

示例 2:

输入: A = "ab", B = "ab"
输出: false

示例 3:

输入: A = "aa", B = "aa"
输出: true

示例 4:

输入: A = "aaaaaaabc", B = "aaaaaaacb"
输出: true

示例 5:

输入: A = "", B = "aa"
输出: false

提示:

  1. 0 <= A.length <= 20000
  2. 0 <= B.length <= 20000
  3. A 和 B 仅由小写字母构成。
public class Solution859 {
public boolean buddyStrings(String A, String B) {
if (A.length() != B.length() || A.length() == 0 && B.length() == 0)
return false; char[] chs1 = A.toCharArray();
char[] chs2 = B.toCharArray();
boolean hasSame = false;
int[] nums = new int[26]; int n1 = -1, n2 = -1;
for (int i = 0; i < chs1.length; i++) {
if (chs1[i] != chs2[i]) {
if (n1 == -1) {
n1 = i;
} else if (n2 == - 1) {
n2 = i;
} else {
return false;
}
} if (!hasSame) {
nums[chs1[i] - 'a'] += 1;
if (nums[chs1[i] - 'a'] > 1)
hasSame = true;
}
} return (n1 == -1 && hasSame) || ((n2 != -1) && (chs1[n1] == chs2[n2] && chs1[n2] == chs2[n1]));
} public static void main(String[] args) {
boolean b = new Solution859().buddyStrings("ab", "ba");
System.out.println(b);
}
}

Q859 亲密字符串的更多相关文章

  1. 力扣(LeetCode)亲密字符串 个人题解

    给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回 false . 示例 1: 输入: A = "ab& ...

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

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

  3. 亲密字符串之Javascript解法

    本题为leetcode第859题,原题链接在此:https://leetcode-cn.com/problems/buddy-strings/submissions/ 给定两个由小写字母构成的字符串  ...

  4. Leetcode859.Buddy Strings亲密字符串

    给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回 false . 示例 1: 输入: A = "ab& ...

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

  6. (c#)亲密字符串

    题目 解

  7. C#LeetCode刷题之#859-亲密字符串​​​​​​​​​​​​​​(Buddy Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3973 访问. 给定两个由小写字母构成的字符串 A 和 B ,只要 ...

  8. C#LeetCode刷题-字符串

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

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 10、差异基因topGO富集

    参考:http://www.biotrainee.com/thread-558-1-1.html http://bioconductor.org/packages/3.7/bioc/ http://w ...

  2. dataview 组件使用示例

    来自<sencha touch 权威指南> ------------------------------- 例子1——app.js代码如下: Ext.require(['Ext.data. ...

  3. 金融卡IC卡知识50问

    1.什么是金融IC卡? 金融IC卡又称为芯片银行卡,是以芯片作为介质的银行卡.芯片卡容量大,可以存储密钥.数字证书.指纹等信息,其工作原理类似于微型计算机,能够同时处理多种功能,为持卡人提供一卡多用的 ...

  4. 《架构师杂志》评述:Scott Guthrie

    发布日期: 2007-03-29 | 更新日期: 2007-03-29   Scott Guthrie 是 Microsoft 开发事业部的总经理.他领导着负责构建 CLR(公共语言运行库).ASP. ...

  5. 前端mock利器:randomjson

    randomjson的应用场景 前后端分离时,前端根据后端提供的数据模型模拟后端请求.如果数据写死,每次返回的都一样,这个时候randomjson就能派上用场了.在前端规定数据类型,每次用random ...

  6. [转]xe6 android 使用距离传感器(Proximiry)

    The first step it's a run sample from RAD Studio that named SensorInfo on your device. On the tab Bi ...

  7. .net Stream篇(六)

    BufferedStream 目录: 简单介绍一下BufferedStream 如何理解缓冲区? BufferedStream的优势 从BufferedStream 中学习装饰模式 如何理解装饰模式 ...

  8. 《spring 攻略》笔记1

    chapter1 spring简介 两种spring ioc容器实现类型: BeanFactory ApplicationContext 应用程序上下文 DI技巧: @Autowired(requir ...

  9. Launch VINS example (Euroc dataset) in RTAB-MAP

    $ roslaunch rtabmap_ros euroc_datasets.launch args:="-d RGBD/CreateOccupancyGrid false Odom/Str ...

  10. 基于redis的分布式锁的分析与实践

    ​ 前言:在分布式环境中,我们经常使用锁来进行并发控制,锁可分为乐观锁和悲观锁,基于数据库版本戳的实现是乐观锁,基于redis或zookeeper的实现可认为是悲观锁了.乐观锁和悲观锁最根本的区别在于 ...