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

题目大意:给出A,B两个字符串,问能否swap一次A串中的两个字符使得A==B

思路:直接找A中和B中不等的字符个数num,如果num==1,或者num==0且A串中有至少两个一样的字符那么就返回YES

代码写的有点乱...

class Solution {
public:
bool buddyStrings(string A, string B) {
string a = A;
string b = B;
sort(A.begin(), A.end());
sort(B.begin(), B.end());
if (A!=B) return false;
int num = 0;
map<char, char> mp;
map<char, int> cnt;
for (int i = 0; i < a.size(); ++i) {
if (a[i] != b[i] && mp[a[i]] != b[i]) {
num++;
mp[b[i]] = a[i];
}
cnt[a[i]]++;
}
if (num == 1) return true;
else if (num == 0) {
for (auto x : cnt){
//cout << x.first <<" " << x.second << endl;
if (x.second >= 2) return true;
}
//cout << "no" << endl;
return false;
}
return false;
}
};

leetcode 859. Buddy Strings的更多相关文章

  1. 859. Buddy Strings - LeetCode

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

  2. 【Leetcode_easy】859. Buddy Strings

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

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

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

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

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

  6. 859. Buddy Strings

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

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

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

  8. [LeetCode] 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)

    这是悦乐书的第330次更新,第354篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第200题(顺位题号是859).给定两个字母A和B的小写字母,当且仅当我们可以在A中交换 ...

随机推荐

  1. mysql赋给用户权限grant all privileges on

    查看mysql用户表的结构,Field项都是各类权限限制 Host限制登录的IP,User限制登录的用户,Delete_priv限制删除权限,Grant_priv限制权限授予,Super_priv为超 ...

  2. http各类攻击及tcpcopy工具

    1.专业的还得ixia.Spirent TestCenter等软硬件一体的 2.一般的使用软件的,安装在linux上使用 参考: 1.http://blog.csdn.net/wuzhimang/ar ...

  3. MFC中 自定义类访问主对话框控件的方法

    之前一直在找有木有好点的方法.现在终于被我找到,收藏之~~~~~~ 在使用mfc的时候经常遇到自定义类访问主对话框控件的问题,例如自定义类中的方法要输出一段字符串到主对话框的EDIT控件.控制对话框的 ...

  4. jsp页面中,动态调用系统时间的实现

    在做WEB项目时,经常会须要 在页面中显示当前时间,以下介绍一个简单的调用系统时间的方法,效果如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvczI5 ...

  5. VELT-0.1.5开发:在VS2013下进行python开发

    快乐虾 http://blog.csdn.net/lights_joy/(QQ群:Visual EmbedLinux Tools 375515651) 欢迎转载,但请保留作者信息 本文仅适用于vs20 ...

  6. 使用matlab进行mex编译时的路径问题mexopts

            matlab和vs 进行混合编程时总须要使用matlab编译mexFunction.cpp文件. 这些文件免不了使用include下的*.h和lib下的*.lib文件.举例说明.这次我 ...

  7. 字符串各个字符ASCII值加5

    程序实现目标: 输入一个字符串,将其各个字符对应的ASCII值加5后,输出结果 程序要求:该字符串只包含小写字母,若其值加5后的字符值大于'z',将其转换成从a开始的字符. 分析:问题归结为三点: 1 ...

  8. 图片压缩CompressUtil解析

    CompressUtil 流程图: CompressUtil 类 具体解释 public class CompressUtil { /** * 终于封装的压缩方法 * @param imgPath * ...

  9. poj 1163 The Triangle 记忆化搜索

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44998   Accepted: 27175 De ...

  10. 09-redis事务及锁应用

    Redis 中的事务 Redis支持简单的事务 Redis与 mysql事务的对比 ------------------------------------------------------- My ...