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:

  1. Input: A = "ab", B = "ba"
  2. Output: true

Example 2:

  1. Input: A = "ab", B = "ab"
  2. Output: false

Example 3:

  1. Input: A = "aa", B = "aa"
  2. Output: true

Example 4:

  1. Input: A = "aaaaaaabc", B = "aaaaaaacb"
  2. Output: true

Example 5:

  1. Input: A = "", B = "aa"
  2. Output: false

wrong case: aa aa, ab cd, abc dew,

What a shame!

  1. class Solution {
  2. public boolean buddyStrings(String A, String B) {
  3. int a1[] =new int[2];
  4. int a2[] =new int[2];
  5. if(A.length()!=B.length() || A.length()==0 || B.length()==0) return false;
  6. int count = 0;
  7. for(int i = 0; i<A.length(); i++){
  8. if(A.charAt(i) != B.charAt(i)) {
  9. count++;
  10. if(count > 2) return false;
  11. a1[count-1] = A.charAt(i);
  12. a2[count-1] = B.charAt(i);
  13. }
  14. }
  15. if(count == 2 &&a1[0]==a2[1]&&a1[1]==a2[0]) return true;
  16. //case for aa (A==B and duplicate elements in the String)
  17. int index[] = new int[26];
  18. if(A.equals(B)){
  19. for(int i = 0; i<A.length(); i++){
  20. index[A.charAt(i)-'a']++;
  21. if(index[A.charAt(i)-'a']>=2) return true;
  22. }
  23. return false;
  24. }else return false;
  25.  
  26. }
  27. }

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. vue自定义指令拖动div

    钩子函数一个指令定义对象可以提供如下几个钩子函数:bind:只掉用一次,指令第一次绑定到元素是调用,在这里可以进行一次性的初始化设置inserted:被绑定元素插入父节点时调用(仅保证父节点存在,但不 ...

  2. from表单,图片预览,和表单提交

    <form> <input id="file" class="topsub-file" type="file" name= ...

  3. TCP Nagle算法以及延迟确认(即延迟回复ACK)的学习

    TCP/IP协议中,无论发送多少数据,总是要在数据前面加上协议头,同时,对方接收到数据,也需要发送ACK表示确认.为了尽可能的利用网络带宽,TCP总是希望尽可能的发送足够大的数据. (一个连TCP接会 ...

  4. 多线程编程_CyclicBarrier

    1.类说明: 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时 Cycl ...

  5. 在使用clone()时id保持一致

    大家都知道,同一个HTML页面中,不宜出现1个以上相同名称的id.但有时候需要使用jQuery框架的clone()来复制相同内容(附带样式),假如是使用了id号的获取方式,即$(‘#***’) 那么复 ...

  6. git使用笔记-提高篇

    一.分支.合并 1.合并一个特定提交 a specific commit git cherry-pick commit-id 把commit-id代表的本次提交合并到当前分支,如果有冲突需要解决后,提 ...

  7. ubuntu mongodb报错:mongo - couldn't connect to server 127.0.0.1:27017

    在进入mongo的时候,出现在下面错误信息.那如何解决呢? 标记一下,以便下次理碰的到时候,有个参考. warning: Failed to connect to 127.0.0.1:27017, r ...

  8. Murano Weekly Meeting 2015.10.06

    Meeting time:  2015.October.6th 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summar ...

  9. 【Linux】Debian 8 设置命令行界面的文本颜色

    平时我们操作的系统命令行界面文本默认黑底白字,有时候会看不惯这种全篇都是白色字符,这个时候可以通过改变PS1环境变量来改变文本颜色.我个人喜欢黑底绿字的搭配,以下是我个人的命令行界面样式: 注意:以下 ...

  10. 动态LINQ(Lambda表达式)构建

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...