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. AlphaGo GITHUB

    AlphaGo GITHUB https://github.com/Rochester-NRT/AlphaGo

  2. Exception:System.Threading.SemaphoreFullException

    ylbtech-Error-Exception-C#: System.Threading.SemaphoreFullException    1.A,异常类型返回顶部 1,异常名称System.Thr ...

  3. 2017.3.14 activiti实战--第二十章--REST服务

    学习资料:<Activiti实战> 第二十章 REST服务 20.1 通信协议概述 略. 20.2 REST API概述 资源分类 资源基础URI 说明 Deployments manag ...

  4. 更新到mysql 5.7后解决0000-00-00日期问题

    更新到mysql 5.7后解决0000-00-00日期问题 学习了:http://www.07net01.com/2016/04/1479450.html mysql 5.7 默认开始用以下sql m ...

  5. iOS动画中的物理知识应用之愤慨的小鸟-碰撞检測

    碰撞检測 源码:https://github.com/Esdeath/collsion 我相信搞iOS得人.多多少少都知道 弹球这个小游戏. 撞击不同的点,就能改变其运动的轨迹.对于非常多人来说,假设 ...

  6. Ajax学习(一)——与Ajax的初次相识

        AJAX是"Asynchronous Javascript And XML"的缩写,从字面上解释是"异步JavaScript和XML"的简称. 它不是一 ...

  7. pyhton3 一些排序算法概括

    1.冒泡算法 import random import datetime def maopao(data): # 检测是否排序完成 for i in range(len(data)-1): flag ...

  8. Android MarginLeft与MarginStart的差别

    在写layout布局的时候,我们会发现有这样几个比較相似的属性: MarginStart   MarginLeft MarginEnd    MarginRight 这些属性的差别是什么?  依据ap ...

  9. 可执行Jar包调用动态链接库(DLL/SO)

    踩过了很多的坑,查了很多资料,在此记录一下,以SpringBoot项目为基础. Maven加入JNA依赖 <!-- JNA start --> <dependency> < ...

  10. C#使用for循环移除HTML标记

    public static string StripTagsCharArray(string source) { char[] array = new char[source.Length]; int ...