作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/buddy-strings/description/

题目描述

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

Note:

  • 0 <= A.length <= 20000
  • 0 <= B.length <= 20000
  • A and B consist only of lowercase letters.

题目大意

当且仅当交换两个字符串中的两个字符的时候,看两个字符串能否完全相等。

解题方法

字典

分析如下:

  1. 如果两个字符串长度不等,那么一定不满足条件
  2. 如果两个字符串完全相等,如果其中存在至少两个相等字符,那么满足条件
  3. 如果两个字符串长度相等且只有两个位置的字符不等,记录下这两个位置,如果这两个字符串的该两个位置字符是恰好错位的,那么满足条件。

代码如下:

class Solution:
def buddyStrings(self, A, B):
"""
:type A: str
:type B: str
:rtype: bool
"""
if len(A) != len(B):
return False
diff = 0
idxs = []
for i, a in enumerate(A):
if B[i] != a:
diff += 1
idxs.append(i)
counter = dict()
if diff == 0:
for a in A:
if a in counter and counter[a]:
return True
else:
counter[a] = True
if diff != 2:
return False
return A[idxs[0]] == B[idxs[1]] and A[idxs[1]] == B[idxs[0]]

C++版本如下:

class Solution {
public:
bool buddyStrings(string A, string B) {
if (A.size() != B.size()) return false;
vector<int> ca(26);
vector<int> cb(26);
int N = A.size();
int diff = 0;
for (int i = 0; i < N; i++) {
if (A[i] != B[i] && diff++ > 2) return false;
ca[A[i] - 'a']++;
cb[B[i] - 'a']++;
}
for(int i = 0; i < 26; i++) {
if (diff == 0 && ca[i] > 1) return true;
if (ca[i] != cb[i]) return false;
}
return diff == 2;
}
};

参考资料:https://zxi.mytechroad.com/blog/string/leetcode-859-buddy-strings/

日期

2018 年 7 月 4 日 —— 夏天挺热的,记得吃饭,防止低血糖
2018 年 11 月 30 日 —— 又到了周末

【LeetCode】859. Buddy Strings 解题报告(Python & C++)的更多相关文章

  1. leetcode 859. Buddy Strings

    Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. 859. Buddy Strings - LeetCode

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

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【Leetcode_easy】859. Buddy Strings

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

  7. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  8. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  9. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

随机推荐

  1. Redis—怎么查看Linux有没有安装Redis,如何启动Redis

    1.检测是否有安装redis-cli和redis-server [root@localhost bin]# whereis redis-cli redis-cli: /usr/bin/redis-cl ...

  2. Redis | 第9章 Lua 脚本与排序《Redis设计与实现》

    目录 前言 1. Lua 脚本 1.1 Redis 创建并修改 Lua 环境的步骤 1.2 Lua 环境协作组件 1.3 EVAL 命令的实现 1.4 EVALSHA 命令的实现 1.5 脚本管理命令 ...

  3. Copy constructor vs assignment operator in C++

    Difficulty Level: Rookie Consider the following C++ program. 1 #include<iostream> 2 #include&l ...

  4. Linux:cp -rp

    cp -rp[原文件或目录] [目标文件或目录] -r   复制目录 - p   保留文件属性 范例: cp -r /yy/k /yy/u /mm 复制目录u和目录k到目录mm中 cp -r /yy/ ...

  5. 商城项目的购物车模块的实现------通过session实现

    1.新建购物车的实体类Cart public class Cart implements java.io.Serializable{ private Shangpin shangpin;//存放商品实 ...

  6. hibernate多对多单向(双向)关系映射

    n-n(多对多)的关联关系必须通过连接表实现.下面以商品种类和商品之间的关系,即一个商品种类下面可以有多种商品,一种商品又可以属于多个商品种类,分别介绍单向的n-n关联关系和双向的n-n关联关系. 单 ...

  7. 声临其境,轻松几步教你把音频变成3D环绕音

    在音乐创作.音视频剪辑和游戏等领域中,给用户带来沉浸式音频体验越来越重要.开发者如何在应用内打造3D环绕声效?华为音频编辑服务6.2.0版本此次带来了空间动态渲染功能,可以将人声.乐器等音频元素渲染到 ...

  8. Redis学习推荐资料合集

    目录 一.官方 二.书籍 三.推荐博客 一.官方 redis官网 redis中文网 redis中文命令网 redis模块 redis-github源码 redis报告 二.书籍 <Redis开发 ...

  9. A New Discrete Particle Swarm Optimization Algorithm

    题目:一种新的离散粒子群优化算法 中文摘要 粒子群优化算法在许多优化问题上表现得非常好.粒子群优化算法的缺点之一是假设算法中的变量为连续变量.本文提出一个新的粒子群优化算法,能够优化离散变量.这个新算 ...

  10. 数据类型Table.TransformColumnTypes(Power Query 之 M 语言)

    数据源: 任意数据源 目标: 设置适合的数据类型 操作过程: 选取指定列>[主页]>[数据类型]>选取 选取指定列>[转换]>[数据类型]>选取 选取指定列> ...