leetcode 859. Buddy Strings
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的更多相关文章
- 859. Buddy Strings - LeetCode
Question 859. Buddy Strings Solution 题目大意: 两个字符串,其中一个字符串任意两个字符互换后与另一个字符串相等,只能互换一次 思路: diff 记录不同字符数 两 ...
- 【Leetcode_easy】859. Buddy Strings
problem 859. Buddy Strings solution: class Solution { public: bool buddyStrings(string A, string B) ...
- 【LeetCode】859. Buddy Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- [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 ...
- 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 ...
- 859. Buddy Strings
class Solution { public: bool buddyStrings(string A, string B) { int lenA=A.length(); int lenB=B.len ...
- LeetCode 859. 亲密字符串(Buddy Strings) 23
859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...
- [LeetCode] Buddy Strings 伙计字符串
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- LeetCode.859-伙伴字符串(Buddy Strings)
这是悦乐书的第330次更新,第354篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第200题(顺位题号是859).给定两个字母A和B的小写字母,当且仅当我们可以在A中交换 ...
随机推荐
- linux下怎样对串口编程
Linux操作系统从一開始就对串行口提供了非常好的支持,本文就Linux下的串行口通讯编程进行简单的介绍. 串口简单介绍串行口是计算机一种经常使用的接口.具有连接线少.通讯简单.得到广泛的使用. 经常 ...
- yii2操作数据库 mysql 读写分离 主从复制
转载地址:http://www.kuitao8.com/20150115/3471.shtml 开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的&quo ...
- MySQL ERROR 1044 (42000) 解决方法
在Terminal中输入 mysql 进入到数据库命令行,然后直接: CREATE DATABASE IF NOT EXISTS yuntu; 结果出现如下错误: ERROR 1044 (42000) ...
- TP框架中多条件筛选
$pid =I('pid'); $year = I('year'); $productType = I('productType'); ...
- 文件I/O操作为什么叫输入/出流
参考以下文档: http://blog.csdn.net/hguisu/article/details/7418161 我们关注的焦点是错误的,重点不在文件,我们关注的核心是数据流. 这种流可以是文本 ...
- JS创建表单提交备份
//保存 function saveFT() { var data = { createDate: GetDateStr(0), name: $("#txtName").val() ...
- Axure教程:滑动进度条、圆形进度环的复杂交互效果实现方法
滑动条.进度条.进度环,是产品原型中比较常见的进度展示功能.今天笔者分享的是使用Axure原型工具实现两种进度展示功能中相对复杂的交互效果. 效果一.可拖动.可显示进度值.可计算多个页面均值的滑动进度 ...
- openstack 用nova API 指定 compute node 创建 instance
感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- Hibernate学习三----------session详解
© 版权声明:本文为博主原创文章,转载请注明出处 如何获取session对象 1. openSession 2. getCurrentSession - 如果使用getCurrentSession需要 ...
- python3.x中xml.etree.ElementTree解析xml举例
1.新建xml import xml.etree.ElementTree as ETa=ET.Element('elem')c=ET.SubElement(a,'child1')c.text=&quo ...