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. Apache+PHP+MySQL环境搭建

    准备安装包:Apache: apache_2.2.11-win32.msi (http://pan.baidu.com/s/1nvdiNcH)PHP: php-5.2.5-Win32.zip (htt ...

  2. 互联网进行限流策略的Semaphore信号量使用

    在Semaphore信号量非常适合高并发访问,新系统在上线之前,要对系统的访问量进行评估,当然这个值肯定不是随便拍拍脑袋就能想出来的,是经过以往的经验.数据.历年的访问量,已经推广力度进行一个合理的评 ...

  3. Python: subprocess.Popen()不支持unicode问题解决

    起源: 所下载视频,有音视频分离者,需要合并起来,采用python之subprocess.Popen()调用ffmpeg实现.python版本为2.7.13,而音视频文件路径,有unicode字符者, ...

  4. 893. Groups of Special-Equivalent Strings 奇数偶数位上的相同数

    [抄题]: You are given an array A of strings. Two strings S and T are special-equivalent if after any n ...

  5. swift - 启动APP 黑屏

    https://blog.csdn.net/chengkaizone/article/details/50478045

  6. Java01-Java基本概念及JDK安装

    Java是由sun公司于1995年5月推出的Java程序设计语言和Java平台的总称. Java是一个完整的平台,不仅提供了优秀的编程语言,而且还提供了大量的可重用代码以及一个能提供安全性.可移植性. ...

  7. pthreads v3下一些坑和需要注意的地方

    一.子线程无法访问父线程的全局变量,但父线程可以访问子线程的变量 <?php class Task extends Thread { public $data; public function ...

  8. centos7下docker1.12.5学习笔记

    一.Docker简介 Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相 ...

  9. socket错误代码

    Socket error 0 - Directly send error Socket error 10004 - Interrupted function call一个封锁操作被对 WSACance ...

  10. java8 数据结构的改变(二) 对ConcurrentHashMap影响

    https://www.cnblogs.com/study-everyday/p/6430462.html http://www.importnew.com/22007.html