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 ...
随机推荐
- c#工具类之Int扩展类
public static class IntHelper { /// <summary> /// 转换为2进制字符串 /// </summary> /// <param ...
- 封装OkHttp,通过改造Callback接口实现
1:实现Callback回调接口import android.os.Handler;import android.os.Looper;import android.os.Message; import ...
- 通过zookeeper连接hive beeline
beeline -u 'jdbc:hive2://zk01:2181,zk02:2181,zk03:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperName ...
- python学习:python文件中空格和换行符的捕获和文本文件的转存
0. 背景 之前公司的项目中,需要在嵌入式系统中实现一个http的网页端内容,由于项目历史遗留问题,公司是采用的将html文件转成c语言头文件的方式,每次修改页面端都需要从新编译一下程序,非常的繁琐. ...
- my.资料__2017暑假
1.http://tieba.baidu.com/p/5254412093 http://www.pipaw.com/mhxy/345616.html [落英缤纷的树下] 地址:长寿村123,24 [ ...
- 干货-Spring3.2.3的所有注解
在用到Spring3的时候,我们需要对耦合的struts2的Action层或者SpringMVC的Controller层加上注解,一般是@Controller和@RequestMapping 看到这里 ...
- Dev Express Report 学习总结(六)Dev Express Reports自定义Summary
在我们使用DevExpress开发报表的过程中,对于页面中复杂的数据合计,我们可能会使用到自定义Summary.下面通过一个例子来进行说明: 首先,我建立了如上图所示的报表页面,其中的数据源来自cla ...
- HBuilder的常用快捷键
Ctrl + d 删除整行内容 Ctrl + Shift +R 复制当前行到下一行 Ctrl + Shift +D 重新编辑 Ctrl + 方向键 当前行整行内容上移或下移 Alt + ↓ 跳转到下一 ...
- Murano Weekly Meeting 2016.05.10
Meeting time: 2016.May.10 1:00~2:00 Chairperson: Serg Melikyan, from Mirantis Meeting summary: 1. m ...
- 聊一聊IAR的workspace文件组织
抽空偶尔做个zigbee实验其实也挺好玩的,今天我就来总结一下嵌入式IAR Embedded Workbench这个非常有效的集成开发环境的文件组织. 每一个workspace由一般是由.c文件和一个 ...