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. js对键盘输入事件绑定到特定按钮

    转自:https://www.cnblogs.com/liluping860122/archive/2013/05/25/3099103.html<script type="text/ ...

  2. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  3. Codeforces Beta Round #61 (Div. 2)

    Codeforces Beta Round #61 (Div. 2) http://codeforces.com/contest/66 A 输入用long double #include<bit ...

  4. 惠普/aruba交换机

    1.开启CDP cdp run show cdp neighbors可看到各端口上.下联的AP或交换机 2.配置telnet管理 1)配置IP地址 vlan 77  name "VLAN77 ...

  5. ABAP中不修改内表参照的结构,给内表/结构体增加字段

    Situation: DATA:       itab  TYPE STANDARD TABLE OF zsrsodtla_stru1,      wa_itab TYPE zsrsodtla_str ...

  6. java 线程Thread 技术--方法演示生产与消费模式

    利用wait 与notifyAll 方法进行演示生产与消费的模式的演示,我们两个线程负责生产,两个线程消费,只有生产了才能消费: 在effective Java 中有说过: 1. 在Java 中 ,使 ...

  7. (六)ROS话题---节点之间通信的方式

    1. 理解 ROS 话题: (Ctrl+Alt+T 打开一个新终端) 运行下面的命令: $ roscore (Ctrl+Alt+T 打开一个新终端) $ rosrun turtlesim turtle ...

  8. 关于js动画简单理解;

    1.CSS样式提供了运动 过度的属性:transition 过度的属性值:attr  ,time  , liner  ,  delay: 值分别是:属性(css),花费的时间,变化的速度(默认匀速), ...

  9. 10.31JS日记

    this问题 (1)this是js的一个关键字,指定一个对象,然后替代this: 函数中的this指向行为发生的主体,函数外的this都指向window,没有意义 (2)函数内的this跟函数在什么环 ...

  10. Lftp 简单使用步骤

    有一用户需要在Linux上面将数据备份至存储上面,但是不支持挂载. 只可以FTP. 找了下,Lftp 这款FTP Client 满足用户的需求. 相关资料参考:http://blog.chinauni ...