[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, ...
随机推荐
- maven 配置mirror后,本地库与远端库冲突
settings.xml中主要包括以下元素: localRepository interavtiveMode offline pluginGroups proxies servers mirrors ...
- 三剑客之awk
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...
- C#调用VB进行简繁转换
首先在C#项目中引用Microsoft.VisualBasic.dll,版本自己选择合适的 然后在项目中添加引用:using Microsoft.VisualBasic; 转换: 转为繁体: outp ...
- hdparm命令(转)
转自:http://man.linuxde.net/hdparm hdparm命令提供了一个命令行的接口用于读取和设置IDE或SCSI硬盘参数. 语法 hdparm(选项)(参数) 选项 -a< ...
- CSS 小技巧
CSS 小技巧 一.边框内圆角 我们在设计例如按钮等控件的时候,会遇到这样的设计:只有内侧有圆角,而边框或者描边的四个角还是保持直角的形状,用以下代码可以轻松的实现. #wrapper { width ...
- vim操作表
- confd template src格式和 templates 语法
Template Resources Template resources are written in TOML and define a single template resource. Tem ...
- 无法跨越程序集边界使用程序集“DataCheck, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中的类型“List<ILayer>”,因为该类型有一个为嵌入互操作类型的泛型类型参数
主窗体: FrmDataInspect f2 = new FrmDataInspect(listMapControl1Lyr); f2.Show(); 弹出的窗体: 应该改为: gListMapLyr ...
- [bigdata] palantir
Palantir的无缝数据融合技术关键在于本体数据模型的灵活性,动态性,而且要能反映人.事.物和环境的关联关系及因果联系,这是大数据技术面临的核心挑战.
- Servlet重写init(ServletConfig config)还是init()
原文地址:Servlet重写init(ServletConfig config)还是init() 写一个Servlet时,有时需要我们重写该Servlet的初始化方法,然后,究竟是重写init(Ser ...