给定两个由小写字母构成的字符串 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. Windows下用Nginx配置遇到的问题

    Nginx是一款轻量级的web服务器/反向代理服务器,更详细的释义自己百度了.目前国内像新浪.网易等都在使用它.先说下我的服务器软件环境: 系统:Windows Server + IIS + ngin ...

  2. Smarty3——变量修饰器

    变量修饰器可以用于变量, 自定义函数或者字符串. 使用修饰器,需要在变量的后面加上|(竖线)并且跟着修饰器名称. 修饰器可能还会有附加的参数以便达到效果. 参数会跟着修饰器名称,用:(冒号)分开. 同 ...

  3. Jlabel实现内容自动换行

    Jlabel实现内容自动换行 摘自:https://blog.csdn.net/zhhtao89/article/details/50179695   2015年12月04日 21:09:27 阅读数 ...

  4. Spark的job调优(1)

    本文翻译之cloudera的博客,本系列有两篇,第二篇看心情了 概论 当我们理解了transformation,action和rdd后,我们就可以写一些基础的spark的应用了,但是如果需要对应用进行 ...

  5. Edge 自动给数字加下划线的问题

    <meta name="format-detection" content="telephone=no,email=no,address=no">

  6. 如何偷Android的内存-Tricking Android MemoryFile

    之前在做一个内存优化的时候,使用到了MemoryFile,由此发现了MemoryFile的一些特性以及一个非常trickly的使用方法,因此在这里记录一下 What is it MemoryFile是 ...

  7. 基于HTML5 Ajax文件上传进度条如何实现(jquery版本)

    <!DOCTYPE html> <html> <head> <title>html5_2.html</title> <meta htt ...

  8. POJ3041 Asteroids(二分图最小点覆盖)

    Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...

  9. 用原生css实现高斯模糊、黑白等滤镜效果

    —引导— 在CSS3中,有一个强大的属性,那就是filter属性,filter顾名思义就是“滤镜”的意思,用filter属性可以让图片无需PS处理就达到一些简单的显示效果. —定义和使用— filte ...

  10. NSProcessInfo系统进程信息

    前言 NSProcessInfo 类中包含一些方法,允许你设置或检索正在运行的应用程序(即进程)的各种类型的信息. 1.获取系统进程信息 // 创建系统进程信息对象 NSProcessInfo *pr ...