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 ,只要 ...
随机推荐
- matlab中fmincon函数求解非线性规划问题
Matlab求解非线性规划,fmincon函数的用法总结 1.简介 在matlab中,fmincon函数可以求解带约束的非线性多变量函数(Constrained nonlinear multivari ...
- 以太网EMC(浪涌)中心抽头方案(节约空间)
- .NET Best Practices: Architecture & Design Patterns (5 Days Training)
.NET Best Practices: Architecture & Design Patterns (5 Days Training) .NET最佳实践:架构及设计模式 5天培训课程 课程 ...
- vue入门文章
本来想自己写一篇关于vue入门的文章.但是看到链接的文章后,觉得写得太详细了,实在有保存下来的必要.后面可能在这篇文章基础上,有所内容的增加. CSS预处理器 定义了一种新的专门的编程语言,编译后成正 ...
- 前端每日实战:134# 视频演示如何用 CSS 和 GSAP 创作一个树枝发芽的 loader
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/LJmpXZ 可交互视频 此视频是可 ...
- 小程序wx.previewImage查看图片再次点击返回时重新加载页面问题
wx.previewImage预览图片这个过程到底发生了什么? 首先我们点击图片预览,附上查看图片代码: <image class="headImg" data-src=&q ...
- win10设置开机自启动程序
问题情境:前两天刚刚给自己的win10系统美化了一下,但发现一个问题,每次开机都需要双击启动一个程序,才能达到一个我想要的效果,所以就在思考能不能将这个程序设为开机自启动项呢? 1.首先,找到启动文件 ...
- Spring原始注解开发-02
使用@Repository.@Service.@Controller注解配置,使其更加清晰属于哪一层,因为我是模拟的web层,所有没有使用@Controller注解,后面结合web开发会使用到 1.创 ...
- kubectl get node -n wide --show-labels
集群环境:1.k8s用的是二进制方式安装2.操作系统是linux (centos)3.操作系统版本为 7.4/7.94.k8s的应用管理.node管理.pod管理等用rancher.k8s令牌以及ma ...
- docker安装elastic search和kibana
安装目标 使用docker安装elastic search和kibana,版本均为7.17.1 安装es 1. docker pull 去dockerhub看具体版本,这里用7.17.1 docker ...