Q859 亲密字符串
给定两个由小写字母构成的字符串 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
提示:
0 <= A.length <= 20000
0 <= B.length <= 20000
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 亲密字符串的更多相关文章
- 力扣(LeetCode)亲密字符串 个人题解
给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回 false . 示例 1: 输入: A = "ab& ...
- LeetCode 859. 亲密字符串(Buddy Strings) 23
859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...
- 亲密字符串之Javascript解法
本题为leetcode第859题,原题链接在此:https://leetcode-cn.com/problems/buddy-strings/submissions/ 给定两个由小写字母构成的字符串 ...
- Leetcode859.Buddy Strings亲密字符串
给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回 false . 示例 1: 输入: A = "ab& ...
- [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 ...
- (c#)亲密字符串
题目 解
- C#LeetCode刷题之#859-亲密字符串(Buddy Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3973 访问. 给定两个由小写字母构成的字符串 A 和 B ,只要 ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- Learning from Imbalanced Classes
https://www.svds.com/learning-imbalanced-classes/ 下采样即 从大类负类中随机取一部分,跟正类(小类)个数相同,优点就是降低了内存大小,速度快! htt ...
- 认识Session
Session在不同环境下的不同含义 session,中文经常翻译为会话,其本来的含义是指有始有终的一系列动作/消息,比如打电话是从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个session ...
- Django-Web框架之Hello Django!
1.首先我们配置guest目录下面的settings.py问,将我们的sign应用添加到项目中.如下图所示: 2.我们通过地址栏:http://127.0.0.1:8001/index/访问Djang ...
- Session.Abandon-Session.Clear-Session.RemoveAll
System.Web.UI.Page.Session属性和System.Web.HttpContext.Session属性 都是System.Web.SessionState.HttpSessionS ...
- eclipse启动了tomcat,但是浏览器打不开欢迎页
tomcat在eclipse中启动成功,主页却打不开 症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他 ...
- Func和Action的介绍及其用法
Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如在反射中使用就可以弥补反射所损失的性能. ...
- 装饰(Decorator)模式
一. 装饰(Decorator)模式 装饰(Decorator)模式又名包装(Wrapper)模式[GOF95].装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 二. 装饰模式 ...
- small cell 在安防领域的应用探讨
在安防领域,最核心的问题是:如何有效区分“内部人员”与“外部人员”.所谓“有效”包含两点意思,一是安全,尽可能地过滤出“外部人员”.二是效率,即尽可能无干扰地或较小干扰地使“内部人员”通过.所有的安全 ...
- ping包,支持ip录入
@echo off ::等待用户输入需要监控IP set /p ip=Input the IP required to monitor: echo executing...... :start ech ...
- Vue 自定义header
第一种,全局设置: Vue.http.headers.common['token'] = 'YXBpOnBhc3N3b3Jk'; 第二种,拦截器设置 Vue.http.interceptors.pus ...