859. Buddy Strings - LeetCode
Question


Solution
题目大意:
两个字符串,其中一个字符串任意两个字符互换后与另一个字符串相等,只能互换一次
思路:
diff 记录不同字符数
两个字符串长度不同 return false
两个字符串长度相同:
abc abd 无重复,diff == 1 ca[3] != cb[3] return false
abc abc 无重复,diff == 0 return false
ab ba 无重复,diff == 2 return true
aa aa aa重复,diff == 0 return true
aabc aabc aa重复,diff == 0 return true
aacb aabc aa重复,diff == 2 return true
Java实现:
public boolean buddyStrings(String A, String B) {
if (A.length() != B.length()) return false;
int[] ca = new int[26];
int[] cb = new int[26];
int diff = 0;
for (int i = 0; i < A.length(); i++) {
ca[A.charAt(i) - 'a']++;
cb[B.charAt(i) - 'a']++;
if (A.charAt(i) != B.charAt(i)) diff++;
}
for (int i = 0; i < ca.length; i++) {
if (diff == 0 && ca[i] > 1) return true;
if (ca[i] != cb[i]) return false;
}
return diff == 2;
}
859. Buddy Strings - LeetCode的更多相关文章
- 【Leetcode_easy】859. Buddy Strings
problem 859. Buddy Strings solution: class Solution { public: bool buddyStrings(string A, string B) ...
- 【LeetCode】859. Buddy Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- leetcode 859. Buddy Strings
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- 859. Buddy Strings (wrong 4 times so many cases to test and consider) if else**
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- 859. Buddy Strings
class Solution { public: bool buddyStrings(string A, string B) { int lenA=A.length(); int lenB=B.len ...
- LeetCode 859. 亲密字符串(Buddy Strings) 23
859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...
- [LeetCode] Buddy Strings 伙计字符串
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- LeetCode.859-伙伴字符串(Buddy Strings)
这是悦乐书的第330次更新,第354篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第200题(顺位题号是859).给定两个字母A和B的小写字母,当且仅当我们可以在A中交换 ...
- C#LeetCode刷题之#859-亲密字符串(Buddy Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3973 访问. 给定两个由小写字母构成的字符串 A 和 B ,只要 ...
随机推荐
- 与和或(&&和||)比较的区别
&&(短路与)和&(逻辑与)的时候: 有假则为假,全真则为真(有假必假,全真为真) ||(短路或)和|(逻辑或)的时候: 有真则为真,全假则为假(有真必真,全假为假)
- (stm32f103学习总结)—独立看门狗(IWDG)
一.IWDG介绍 1.1 IWDG简介 STM32F1芯片内部含有两个看门狗外设,一个是独立看门狗IWDG,另 一个是窗口看门狗WWDG.两个看门狗外设(独立和窗口)均可用于检测 并解决由软件错误导致 ...
- HTML5 Canvas绘制效率如何?
js运行效率在提升 编程语言的效率是前提,js自然比不上native的C语言效率,所以Canvas效率无疑比不上原生的2D图形绘制,但是js效率的提升是有目共睹的,以js与as为例,基本操作(运算操作 ...
- 微信小程序加密数据(encryptedData)解密中的PHP代码,php7.1报错
问题描述 最近在开发微信小程序涉及到加密数据(encryptedData)的解密,用的是PHP代码,在运行后报错mcrypt_module_ xxx is deprecated,提示方法已过时了 经研 ...
- HTML 初学整理
一.HTML简介 HTML的概念 HTML是HyperText Markup Language(超文本标记语言)的简写,超文本标记语言,标准通用标记语言下的一个应用."超文本"就是 ...
- activity-alias属性的使用
activity-alias是Android里为了重复使用Activity而设计的.1. 含义和作用: 对于activity-alias标签,它有一个属性叫android:targen ...
- CCF201812-2小明放学
题目背景 汉东省政法大学附属中学所在的光明区最近实施了名为"智慧光明"的智慧城市项目.具体到交通领域,通过"智慧光明"终端,可以看到光明区所有红绿灯此时此刻的状 ...
- java中将科学技术发转为正常数据
import java.text.NumberFormat; public class test { public static void main(String[] args) { double d ...
- css实现半圆效果
效果图: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- 使用Object.Defineproperties改变对象数据结构
此方法设置键的时候如果需要使键为变量则需要加中括号[] 如下 let addKeys = Number(keys[keys.length - 1]) + 1 Object.assign(this.t ...