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. Slapper帮助Dapper实现一对多

    Dapper的Query的方法提供了多个泛型重载可以帮助我们实现导航属性的查询 1对1 public class Employees4List { public int Id { get; set; ...

  2. 数据库系统Informix为例,介绍改善用户查询计划的方法。

    数据库系统Informix为例,介绍改善用户查询计划的方法. 1.合理使用索引 索引是数据库中重要的数据结构,它的根本目的就是为了提高查询效率.现在大多数的数据库产品都采用IBM最先提出的ISAM索引 ...

  3. HDU - 6231 K-th Number (2017CCPC哈尔滨站 二分+尺取法)

    Alice are given an array A[1..N] with N numbers. Now Alice want to build an array B by a parameter K ...

  4. lxml基础

    节点操作: from lxml import etree # 1.创建Element对象,参数即节点名称 root = etree.Element('root') print(root) # < ...

  5. [No0000B1]ReSharper操作指南2/16-ReSharper食谱与特定于域的教程

    自动导入名称空间 有关更多信息,请参阅导入缺少命名空间. 每当您使用未添加using语句的命名空间中的类型时,ReSharper会为您提供在您所在文件的顶部添加相应的语句.这由在所使用的类型上方显示的 ...

  6. centos6.5(64bit),python2.6.6安装MySQLdb模块

    1.下载MySQL-python.setuptools安装包 2.tar zxvf setuptools-0.6c11.tar.gz 3.cd setuptools-0.6c11 4.python s ...

  7. [DPDK] 转发 DPDK分析

    前半部分,对于背景和需求的分析,写的相当好啊! 原文地址:https://www.jianshu.com/p/0ff8cb4deaef 背景分析 前10年中,网络程序性能优化的目标主要是为了解决C10 ...

  8. 《Mysql 引擎》

    一:什么是引擎? - 就是一种数据存取和处理方式. - 在 MySQL 中,引擎是以"插件式"存在的,使我们可以很方便的使用各种引擎. 二:怎么查看数据库支持的引擎? - show ...

  9. Java之旅_高级教程_序列化

    摘自 :http://www.runoob.com/java/java-serialization.html  Java序列化 Java提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字 ...

  10. Java 第二次测试总结

    Java 第二次测试总结 1. 相关知识点总结 Java测试题循环与递归知识点 补充知识点: for循环语句:for(表达式1:表达式2:表达式3)表达式一负责完成变量的初始化!表达式2是值为bool ...