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 in A so that the result equals B.
Example 1:
Input: A = "ab", B = "ba"
Output: true
Example 2:
Input: A = "ab", B = "ab"
Output: false
Example 3:
Input: A = "aa", B = "aa"
Output: true
Example 4:
Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true
Example 5:
Input: A = "", B = "aa"
Output: false
wrong case: aa aa, ab cd, abc dew,
What a shame!
class Solution {
public boolean buddyStrings(String A, String B) {
int a1[] =new int[2];
int a2[] =new int[2];
if(A.length()!=B.length() || A.length()==0 || B.length()==0) return false;
int count = 0;
for(int i = 0; i<A.length(); i++){
if(A.charAt(i) != B.charAt(i)) {
count++;
if(count > 2) return false;
a1[count-1] = A.charAt(i);
a2[count-1] = B.charAt(i);
}
}
if(count == 2 &&a1[0]==a2[1]&&a1[1]==a2[0]) return true;
//case for aa (A==B and duplicate elements in the String)
int index[] = new int[26];
if(A.equals(B)){
for(int i = 0; i<A.length(); i++){
index[A.charAt(i)-'a']++;
if(index[A.charAt(i)-'a']>=2) return true;
}
return false;
}else return false;
}
}
So many if else case to care about! Traps
859. Buddy Strings (wrong 4 times so many cases to test and consider) if else**的更多相关文章
- 【Leetcode_easy】859. Buddy Strings
problem 859. Buddy Strings solution: class Solution { public: bool buddyStrings(string A, string B) ...
- 859. Buddy Strings - LeetCode
Question 859. Buddy Strings Solution 题目大意: 两个字符串,其中一个字符串任意两个字符互换后与另一个字符串相等,只能互换一次 思路: diff 记录不同字符数 两 ...
- 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 ...
- 【LeetCode】859. Buddy Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 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 ...
- [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 ...
- [LeetCode] 859. Buddy Strings_Easy
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
随机推荐
- hdu-4513吉哥系列故事——完美队形II--最长回文
吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- my07_lock-tables与single-transaction的区别
概念描述 ************************************************************ mysqldump进行逻辑备份时(innodb),为保证事务的一致性 ...
- python文件引用其他文件中的变量
问题: 然后再另一个文件中引用该变量 报错:Cannot find reference 'User_Agent' in '__init__.py' less... (Ctrl+F1) 正确写法: fr ...
- docker 命令详解(cp篇)
docker cp :用于容器与主机之间的数据拷贝. 1.从容器里面拷文件到宿主机 答:在宿主机里面执行以下命令 docker cp 容器名:要拷贝的文件在容器里面的路径 要拷贝到宿主机的相应路径 ...
- Django media的设置
django在定义模型时需要一些上传的文件,例如图片 class Banner(models.Model): """ 轮播图models titles 标题 images ...
- Django-5.2 模型层 多表操作
7.3 多表操作 一.创建模型 实例:我们来假定下面这些概念,字段和关系作者模型:一个作者有姓名和年龄.作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之 ...
- Python + Selenium 练习篇 - 获取页面所有邮箱
代码如下: # coding=utf-8import re #python中利用正则,需要导入re模块from selenium import webdriverdriver = webdriv ...
- Unity ContextMenu 上下文菜单
新建脚本: public class ContextTesting : MonoBehaviour { [ContextMenu("哈哈")] void DoSomething() ...
- If you are tired...
如果你累了 1. 深呼吸 放松身体,深呼吸五分钟. 2. 听音乐 静静地听几首歌放松一下就好了,比如王豪学长推荐的追梦赤子心,骄傲的少年. 3. 冥想 放松身体,处于冥想状态. 4. 干洗脸.鸣天鼓. ...
- 最新版本dede与discuz通过ucenter完美整合
人合租虚拟主机.然后到相关的官方网站上面下载相关的程序,我下载的是DedeCmsV5.7-GBK+Discuz_X2_RC_SC_GBK+UCenter_1.6.0_SC_GBK这个程序组合.涉及到怎 ...