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. python模块相关

    aniso8601 pyquery networkx (2.0)                 - Python package for creating and manipulating grap ...

  2. 社区管理有捷径!Wish3D Earth社区网格化管理案例重磅上线

    社区网格化是精细化.全覆盖.高效率的社区管理模式,便捷有效的社区网格化管理平台是社区网格化管理的关键. Wish3D Earth全新上线三维社区网格化管理平台,使用实景三维模型作为地图,地形地貌真实展 ...

  3. 负样本采样及bias校准、ctr平滑

    参考:https://zhuanlan.zhihu.com/p/31529643 在CTR预估中,负样本采样是一种常见的特征工程方法.一般CTR预估的原始正负样本比可能达到1:1000~1:10000 ...

  4. 转: scala语言的简单入门 (IBM develop)

    转: https://www.ibm.com/developerworks/cn/java/j-lo-funinscala2/

  5. Oraclet提交提示Record is locked by another user错误

    http://blog.csdn.net/alifel/article/details/4324338下午修改oracle datebase中的字段时,提示"Record is locked ...

  6. FTP 连接报错

    Filezilla 站点管理器=>选中FTP站点=>加密(只使用普通FTP)

  7. jquery插件获取事件类型

    //需要在使用函数时传入event关键字 $('[name=lprice]').change(function(event){ $('[name=lprice]').validate({ event: ...

  8. 看完这篇再不会 View 的动画框架,我跪搓衣板

    引言 众所周知,一款没有动画的 app,就像没有灵魂的肉体,给用户的体验性很差.现在的 android 在动画效果方面早已空前的发展,1.View 动画框架 2.属性动画框架 3.Drawable 动 ...

  9. HDFS源码分析数据块复制之PendingReplicationBlocks

    PendingReplicationBlocks实现了所有正在复制的数据块的记账工作.它实现以下三个主要功能: 1.记录此时正在复制的块: 2.一种对复制请求进行跟踪的粗粒度计时器: 3.一个定期识别 ...

  10. Angualr 实现复选框全选功能

    html <html lang="en"> <head> <meta charset="UTF-8"> <title& ...