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**的更多相关文章

  1. 【Leetcode_easy】859. Buddy Strings

    problem 859. Buddy Strings solution: class Solution { public: bool buddyStrings(string A, string B) ...

  2. 859. Buddy Strings - LeetCode

    Question 859. Buddy Strings Solution 题目大意: 两个字符串,其中一个字符串任意两个字符互换后与另一个字符串相等,只能互换一次 思路: diff 记录不同字符数 两 ...

  3. 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 ...

  4. 【LeetCode】859. Buddy Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  5. 859. Buddy Strings

    class Solution { public: bool buddyStrings(string A, string B) { int lenA=A.length(); int lenB=B.len ...

  6. LeetCode 859. 亲密字符串(Buddy Strings) 23

    859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...

  7. [LeetCode] Buddy Strings 伙计字符串

    Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. tomcat 虚拟目录配置appBase和docBase的区别

    先看server.xml文件host配置   <Host name="localhost" appBase="webapps"      可以修改成自己想 ...

  2. EF上下文容器,保存线程唯一性

    在工作中有个疑问,就是EF上下文容器到底创建了多少个? 在asp.net中,EF上下文容器.如果只要有一个,则每次一个用户访问,添加一些实体,然后又不会自动销毁,就会造成内存爆炸.如果每次创建一个,则 ...

  3. mapreduce统计总数

    现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1. buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t ...

  4. js和jq中常见的各种位置距离之offset和offset()的区别(三)

    offsetLeft:元素的边框的外边缘距离与已定位的父容器(offsetparent)的左边距离(不包括元素的边框和父容器的边框). offset().left:返回的是相对于当前文档的坐标,使用o ...

  5. win10电脑的USB接口插上U盘以后不能使用?

    今天刚遇到的问题 解决方法如下: 右击“我的电脑”选“属性”,打开“设备管理器”,双击“通用串行总线控制器”,双击任意一个,打开属性对话框,切换到“电源管理”选项卡,去除“允许计算机关闭这个设备以节约 ...

  6. JavaSE---反射(未完待续)

    1.概述 1.1 Java程序中许多对象在运行时会出现2种类型:编译时类型.运行时类型: eg:Person  person=new Student(); 这行代码在编译时为Person类型,运行时为 ...

  7. TCP/IP协议<一>

    下面是协议层从底层至顶层的一个模型图: 一.计算机网络的背景 1.1 计算机的发展 有人说:“20世纪最伟大的发明就是计算机”,自诞生伊始,计算机经历了一系列发展,从大型通用计算机.超级计算机.小型机 ...

  8. vue2.X+elementUI表单自定义验证

    <template> <div class="taxi-appointment-dairen"> <el-form :model="rule ...

  9. SourceTree 关于 .gitignore使用/下载

    # =============== # # Unity generated # # =============== # Temp/ Obj/ UnityGenerated/ Library/ Asse ...

  10. CBoard数据分析实战

    介绍 CBoard由上海楚果信息技术有限公司主导开源, 它不仅仅是一款自助BI数据分析产品, 还是开放的BI产品开发平台: 用户只需简单妥妥拽拽就能自助完成数据多维分析与报表设计 开发者能够简单扩展连 ...