class Solution
{
public:
bool buddyStrings(string A, string B)
{
int lenA=A.length();
int lenB=B.length();
if(lenA!=lenB)
return false;
unordered_set<char>cset(A.begin(),A.end());
if(A==B&&cset.size()<lenA)
return true;
vector<int> diff;
for(int i=;i<lenA;i++)
{
if(A[i]!=B[i])
diff.push_back(i);
}
return diff.size()==&&A[diff[]]==B[diff[]]&&A[diff[]]==B[diff[]];
}
};

分两种情况,一种是A==B,另一种是A!=B

相等前提下,只要A中有两个相同字母,就满足条件

不等前提下,有且只有两个交叉字母才满足条件

859. Buddy Strings的更多相关文章

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

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

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

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

  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. Linux 向文件末尾追加命令

    Linux 向文件末尾追加命令 //echo后边用单引号包围要添加的内容 echo 'add content'>>/home/data/test.sh 注意:>> 是追加 ec ...

  2. Python+Selenium学习--alert/confirm/prompt 处理

    场景 webdriver 中处理JavaScript 所生成的alert.confirm 以及prompt 是很简单的.具体思路是使用switch_to.alert()方法定位到alert/confi ...

  3. mybatis BigDecimal Double Long 的坑爹事

    写接口的时候别用 public Map<String,Double> selectForRealRemainer(Orders orders); 用这样就行 public Map<S ...

  4. 【mysql】字段支持JSON类型

    mysql从5.7开始已经支持JSON类型的字段. 支持的操作:添加,修改,置空,子key添加,子key重置,子key删除,通过子key查找等. 但是这里和普通字段的修改和查找不同,涉及到一些JSON ...

  5. TZOJ 1911 A Plug for UNIX(最大流)

    描述 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...

  6. java 基础之--传统网络编程

    什么是socket ? socket 是连接运行在网络上的两个程序间的双向通讯端点 服务器将某一套接字绑定到一个特定的端口,并通过这一套接字等待和监听客户端的的连接请求 客户端通过这个端口与服务器进行 ...

  7. String.format的用法

    有些时候,对于一些东西,不是没有简单的方法,而是我们没有接触到过 String.format();即创建格式化的字符串,里面有很多的通配使用符号,我这里说一下我接触到的,以后接触到其他的再填坑 它的内 ...

  8. 微信小程序基础架构

    一个微信小程序界面由一个页面描述文件,一个页面逻辑文件,一个样式表文件来进行描述 在主目录中的三个以app开头的文件就是微信小程序的主描述文件 app.js :主逻辑文件,用来注册小程序 app.js ...

  9. 快速排序中BUG int 与 int *

    #include <iostream>using namespace std;int QKPass(int* , int , int);  //若声明为 int QKPass(int, i ...

  10. python调试工具pdb

    pdb是基于命令行的调试工具,非常类似gnu的gdb(调试c/c++). 命令 简写命令 作用 break b 设置断点 continue c 继续执行程序 list l 查看当前行的代码段 step ...