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:

  1. 0 <= A.length <= 20000
  2. 0 <= B.length <= 20000
  3. A and B consist only of lowercase letters.

题目思路就是先用length来初步判断, 然后如果相等的话要看是否有重复的元素, 如果有那就可以, 否则不行, 比如aa, aa可以, ab, ab不行. 如果不等, 就要他们不同的地方正好两个, 并且交换顺序, 正好相等.

Code      T: O(n)    S; O(n)    but if contains lower cases, then at most will be 26 , so you can explain it is O(1).

class Solution:
def buddyStrings(self, A, B):
la, lb = len(A), len(B)
if la != lb: return False
if A == B:
return any(val > 1 for val in collections.Counter(A).values())
ans = [None] * 2
for i in range(la):
if A[i] != B[i]:
if ans[1]:
return False
elif ans[0]:
ans[1]= A[i] + B[i]
else:
ans[0]= B[i] + A[i]
return ans[0] and ans[1] and ans[0] == ans[1]

[LeetCode] 859. Buddy Strings_Easy的更多相关文章

  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 859. 亲密字符串(Buddy Strings) 23

    859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...

  3. 859. Buddy Strings - LeetCode

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

  4. 【Leetcode_easy】859. Buddy Strings

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

  5. 【LeetCode】859. Buddy Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  6. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  7. 859. Buddy Strings

    class Solution { public: bool buddyStrings(string A, string B) { int lenA=A.length(); int lenB=B.len ...

  8. 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 ...

  9. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

随机推荐

  1. LIBXML2库使用指南2

    3. 简单xml操作例子 http://blog.sina.com.cn/s/blog_4673bfa50100b0xj.html 了解以上基本知识之后,就可以进行一些简单的xml操作了.当然,还没有 ...

  2. Flask web开发之路六

    紧接着上篇文档,写模板继承和block,URL链接和加载静态文件 模板继承和block 项目结构 主app文件代码: from flask import Flask,render_template a ...

  3. uni-app,wex5,APPcan,ApiCloud几款国内webapp开发框架的选型对比

    框架列表. https://www.cnblogs.com/xiaxiaxia/articles/5705557.html 前言 近期,要开一个新的项目,APP类型.最重要的需求就是能够随时调整APP ...

  4. easyui---layout实战

    第一步: <div id="cc" class="easyui-layout" fit=true> <div region="nor ...

  5. VS2015 工具箱 保存位置

    我的文档\Visual Studio 2015\Settings\CurrentSettings.vssettings Environment_Toolbox 节点 <Category name ...

  6. 洛谷P1192 台阶问题【记忆化搜索】

    题目:https://www.luogu.org/problemnew/show/P1192 题意: 给定n和k,一个人一次可以迈1~k步,问走n步有多少种方案. 思路: 本来傻乎乎上来就递归,显然会 ...

  7. 洛谷P1219 八皇后【dfs】

    题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列2 4 6 1 3 ...

  8. TOP100summit2017:Riot Games 李仁杰——大数据落地要找到数据和经验的平衡点

      壹佰案例:李仁杰老师您好,很荣幸您能参加第六届TOP100全球软件案例研究峰会,您在大数据和人工智能领域有非常丰富的经验,在这次大会上您将分享什么内容? 李仁杰:这次我主要分享的有两个方面. 一个 ...

  9. 基于LSD的直线提取算法

    https://blog.csdn.net/tianwaifeimao/article/details/17678669 文献翻译:https://blog.csdn.net/YuYunTan/art ...

  10. [No000018F]Vim自动缩进配置、原理和tab键替换空格-Vim使用技巧(4)

    一.Vim缩进介绍 在没有设置Vim自动缩进的条件下,可以手动使用Vim命令对特定行进行缩进处理.在Vim插入模式下,按下 Tab 键时默认会输入一个制表符,可通过Vim配置项将 Tab 替换为空格, ...