class Solution {
public:
bool buddyStrings(string A, string B) {
if (A.length() != B.length()) {
return false;
}
// 交换两个字符使得AB相同
// 检测A和B中不同的字符位置个数
int diff_cnt = ;
int idxs[] = { , };
for (int i = ; i < A.length(); ++i) {
if (A[i] != B[i]) {
idxs[diff_cnt++] = i; }
}
if (diff_cnt == ) {
// 没有不同
// 看A里是否有相同的字符串
map<char, bool> m;
for (auto &ch : A) {
if (m[ch]) {
return true;
}
else {
m[ch] = true;
}
}
return false;
}
if (diff_cnt != ) {
return false;
}
swap(A[idxs[]], A[idxs[]]);
return A == B;
}
};

网上的答案,以备复习使用。

leetcode859的更多相关文章

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

  2. Leetcode859.Buddy Strings亲密字符串

    给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回 false . 示例 1: 输入: A = "ab& ...

随机推荐

  1. Jmeter用表格查看结果

    Sample#:编号类似id Start Time:开始时间 Thread Name:线程名称 Label:请求名称 Sample Time:取样时间ms Status:状态 Bytes:接受字节数 ...

  2. THREE.OrbitControls参数控制

    // Set to false to disable this control//鼠标控制是否可用 this.enabled = true; // "target" sets th ...

  3. Cookie简单实例

    Cookie简单实例 1.创建CookieServlet package com.servlet.study; import java.io.IOException; import java.io.P ...

  4. Codeforces 828C String Reconstruction【并查集巧妙运用】

    LINK 题目大意 给你n个串和在原串中的出现位置,问原串 思路 直接跑肯定是GG 考虑怎么优化 因为保证有解,所以考虑过的点我们就不再考虑 用并查集维护当前每个点之后最早的没有被更新过的点 然后就做 ...

  5. BZOJ5297 CQOI2018 社交网络 【矩阵树定理Matrix-Tree】

    BZOJ5297 CQOI2018 社交网络 Description 当今社会,在社交网络上看朋友的消息已经成为许多人生活的一部分.通常,一个用户在社交网络上发布一条消息(例如微博.状态.Tweet等 ...

  6. 报错 Inferred type 'S' for type parameter 'S' is not within its bound; 解决办法

    出现情况: Inferred type 'S' for type parameter 'S' is not within its bound; should extends xxxxxx 出现这种问题 ...

  7. __weak、__strong这样的关键词和weak、strong有哪些区别

    ios4 设备上最好就不要使用 ARC... strong,weak 用来修饰属性.strong 用来修饰强引用的属性:@property (strong) SomeClass * aObject;  ...

  8. cell的循环利用

    方式1 // 1.先根据cell的标识去缓存池中查找可循环利用的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdent ...

  9. windows 2016 容器管理

    1. docker-compose 安装        python   2.7        pip       pip   install docker-compose        常见问题: ...

  10. jdk、jre、JVM的简单区别与联系

    2015-10-20 23:08:52 (1)jdk Java development toolkit(开发工具包),JDK是整个JAVA的核心,包括了Java运行环境jre(Java Runtime ...