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 ...
随机推荐
- 江西财经大学第一届程序设计竞赛 A
链接:https://www.nowcoder.com/acm/contest/115/A来源:牛客网 题目描述 "挤需体验五番钟,里造会挨上这款游戏!" 怎么可能嘛!当我是傻子吗 ...
- hdu1865 1sting (递归+大数加法)
1sting Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- day_11 生成器
. 生成器本身是迭代器 .写迭代器的方式 .生成器函数 .生成器表达式 .各种推导式 .生成器函数 函数内部有yield, yield返回->return yield把函数分段进行 生成器可以使 ...
- python3 logging笔记
#coding:utf-8import logging logger = logging.getLogger("simple_example")#可以说是日志信息的名字吧,可以随便 ...
- 用NaviCat创建存储过程批量添加测试数据
打开navicat连接上数据库,然后打开左上角函数,新建一个函数. BEGIN DECLARE i int; --声明变量 DECLARE groupid int; set i=LAST_INSERT ...
- 查看Oracle当前连接数
SQL> select count(*) from v$session #当前的连接数 SQL> Select count(*) from v$session where status=' ...
- DevStack添加Swift
# Swift# ----- # Swift is now used as the back-end for the S3-like object store. If Nova's# objectst ...
- Zookeeper分布式集群部署
ZooKeeper 是一个针对大型分布式系统的可靠协调系统:它提供的功能包括:配置维护.名字服务.分布式同步.组服务等: 它的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效.功能稳定的 ...
- java.lang.IllegalArgumentException: Page directive: invalid value for import 问题处理
1.问题说明: 项目原来用的tomcat版本是apache-tomcat-6.0,后来为了安全原因将版本升至apache-tomcat-7.0,发现有的jsp页面出现下面的异常: java.lang. ...
- jQuery 间歇式无缝滚动特效分享(三张图片平行滚动)
最近项目中门户首页需要做出图片间歇式无缝滚动特效,但是在网上找资料都是不太理想,不过可以指导.最后自己写了一个demo实现了这个特效,分享出来. 1.jquery.cxscroll.js /*! * ...