[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 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 <= 200000 <= B.length <= 20000AandBconsist 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的更多相关文章
- 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 ...
- LeetCode 859. 亲密字符串(Buddy Strings) 23
859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...
- 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] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 859. Buddy Strings
class Solution { public: bool buddyStrings(string A, string B) { int lenA=A.length(); int lenB=B.len ...
- 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 ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
随机推荐
- 隐藏响应的server,X-Powered-By
隐藏X-Powered-By 修改 php.ini 文件 设置 expose_php = Off apache 隐藏 server 修改httpd.conf 设置 ServerSignature Of ...
- PAT甲级1075 PAT Judge
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805393241260032 题意: 有m次OJ提交记录,总共有k道 ...
- [No000010E]Git7/9-标签管理
发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一个快照 ...
- [No0000E6]C# 判断与循环
判断语句 语句 描述 if 语句 一个 if 语句 由一个布尔表达式后跟一个或多个语句组成. if...else 语句 一个 if 语句 后可跟一个可选的 else 语句,else 语句在布尔表达式为 ...
- 【编译原理】c++实现自下而上语法分析器
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- acid. cap
BASE是下面三个术语的缩写: 基本可用(Basically Available) 软状态(Soft state) 最终一致(Eventually consistent) 目前最快的KV数据库,10W ...
- linux 循环读取文件的每一行
在Linux中有很多方法逐行读取一个文件的方法,其中最常用的就是下面的脚本里的方法,而且是效率最高,使用最多的方法.为了给大家一个直观的感受,我们将通过生成一个大的文件的方式来检验各种方法的执行效率. ...
- 深入理解为什么应该使用transform来替代top
话说,这个问题我们得从浏览器得渲染机制说起: 我们先来理解一下 重绘(Repainit)和 回流(Reflow): 重绘:当节点需要更改外观而不会影响布局得,比如改变 color 就称为重绘: 回流: ...
- JavaScript学习(八)
- VS Code 添加移除asp.net core项目引用
可以通过编辑.csproj文件来添加或者移除项目引用. 注意这里并没有智能提示, 最好是在Nuget网站(https://www.nuget.org/)搜索好相关包之后填写进来. 编辑结束之后, vs ...