给定两个由小写字母构成的字符串 AB ,只要我们可以通过交换 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. AB 仅由小写字母构成。
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. 487C Prefix Product Sequence

    传送门 题目大意 分析 因为n为质数所以i-1的逆元唯一 因此ai唯一 代码 #include<iostream> #include<cstdio> #include<c ...

  2. Entity Framework 6.0 Tutorials(5):Command Interception

    Interception: Here, you will learn how to intercept EF when it executes database commands. EF 6 prov ...

  3. Java 设计模式 和七大设计原则

    创建型模式 抽象工厂模式(Abstract factory pattern): 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要指定具体类. 生成器模式(Builder pattern): 使 ...

  4. Monkey进行压力测试定位问题分析

    Monkey测试的log分析,我们可以通过几个关键词来判断测试是否通过.   分析log方法一(粗糙一点): 1)Monkey finished打开LOG,查看log的最下端,是否有类似以下字段:## ...

  5. css总结13:CSS 伪类(Pseudo-classes)

    1 伪类作用:CSS伪类是用来添加一些选择器的特殊效果. 2 常用示例: 2.1anchor伪类:代码:   正常语法: a{color:#FF0000;}/* 文字颜色 */   伪类语法: a:l ...

  6. window7 下配置python2.7+tornado3.3开发环境

    玩python的人大都在linux下进行开发,由于长期习惯在windows下开发代码,今天蛋疼尝试在window7下配置python2.7+tornado3.3开发环境,必然的中间遇到各种报错,但是最 ...

  7. 独立部署GeoWebCache

    在进行GIS项目开发中,常使用Geoserver作为开源的地图服务器,Geoserver是一个JavaEE项目,常通过Tomcat进行部署.而GeoWebCache是一个采用Java实现用于缓存WMS ...

  8. Nginx conf基本配置

    #定义Nginx运行的用户和用户组 user www www;   #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8;   #全局错误日志定义类型,[ debu ...

  9. 洛谷P2287 [HNOI2004]最佳包裹(三维凸包)

    题面 传送门 题解 左转板子,调个精度就能\(A\)了 //minamoto #include<bits/stdc++.h> #define R register #define fp(i ...

  10. linux系统安全及应用——弱口令检测

    Joth the Ripper,简称JR,一款密码分析工具,支持字典式的暴力破解,通过对shadow文件的口令分析,可以检测密码强度,官方网站http://www.openwall.com/john/ ...