[Swift]LeetCode859. 亲密字符串 | Buddy Strings

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10588891.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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.

给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true ;否则返回 false 。

示例 1:

输入: A = "ab", B = "ba"
输出: true

示例 2:

输入: A = "ab", B = "ab"
输出: false

示例 3:

输入: A = "aa", B = "aa"
输出: true

示例 4:

输入: A = "aaaaaaabc", B = "aaaaaaacb"
输出: true

示例 5:

输入: A = "", B = "aa"
输出: false

提示:

  1. 0 <= A.length <= 20000
  2. 0 <= B.length <= 20000
  3. A 和 B 仅由小写字母构成。

16ms

 

 1 class Solution {
 2     func buddyStrings(_ A: String, _ B: String) -> Bool {
 3         guard !A.isEmpty,!B.isEmpty, A.count == B.count else {
 4             return false
 5         }
 6         if A == B && Set(A).count < A.count { return true }
 7         let arrayA = Array(A)
 8         let arrayB = Array(B)
 9         var diffIndexArray:Array<Int> = []
10         for i in 0..<A.count {
11             if arrayA[i] != arrayB[i] {
12                 diffIndexArray.append(i)
13             }
14             if diffIndexArray.count > 2 {
15                 return false
16             }
17         }
18         if diffIndexArray.count != 2 { return false }   
19         return arrayA[diffIndexArray[1]] == arrayB[diffIndexArray[0]] && arrayA[diffIndexArray[0]] == arrayB[diffIndexArray[1]]
20     }
21 }

20ms

 1 class Solution {
 2     func buddyStrings(_ As: String, _ Bs: String) -> Bool {    
 3     guard As.count == Bs.count && As.count > 1 else {
 4         return false
 5     }    
 6     if As == Bs {
 7         if Set(As).count < As.count {
 8             return true
 9         }        
10         return false
11     }    
12     let A = Array(As)
13     let B = Array(Bs)
14     var diff = [Int]()
15     for i in 0..<A.count {
16         if A[i] != B[i] {
17             diff.append(i)
18         }        
19         if diff.count > 2 {
20             return false
21         }
22     }    
23     guard diff.count == 2 else {
24         return false
25     }    
26     if  A[diff[0]] == B[diff[1]] && A[diff[1]] == B[diff[0]]  {
27         return true
28     }    
29     return false
30     }
31 }

24ms

 1 class Solution {
 2     func buddyStrings(_ A: String, _ B: String) -> Bool {
 3         if A.length != B.length { return false }
 4         if A.count < 2 || A.count != B.count {
 5             return false
 6         }
 7         if A == B && Set(A).count < A.count {
 8             return true
 9         }
10         
11         var a = Array(A)
12         var b = Array(B)
13         var array = [String]()
14         
15         if A != B {
16             for i in 0 ..< a.count {
17                 if a[i] != b[i] {
18                     array.append(String(a[i]))
19                     array.append(String(b[i]))
20                 }
21             }
22             if array.reversed() == array {
23                 return true
24             }
25         }
26         return false
27     }
28 }

28ms

 1 class Solution {
 2     func buddyStrings(_ A: String, _ B: String) -> Bool {
 3         if A.count != B.count {
 4             return false
 5         }  
 6         var map = [Character: Int]()
 7         var x = -1
 8         var y = -1
 9         let arrayA = Array(A)
10         let arrayB = Array(B)   
11         for i in 0..<A.count {
12             map[arrayA[i], default: 0] += 1           
13             if arrayA[i] == arrayB[i] {
14                 continue
15             }
16             if x == -1 {
17                 x = i
18             }
19             else if y == -1 {
20                 y = i
21             }
22             else {
23                 return false
24             }
25         }
26         if x != -1 && y != -1 {
27             if arrayA[x] == arrayB[y] && arrayA[y] == arrayB[x] {
28                 return true
29             }
30         }
31         else {
32             for (_, value) in map {
33                 if value > 1 {
34                     return true
35                 }
36             }
37         }
38         return false
39     }
40 }

32ms

 1 class Solution {
 2     func buddyStrings(_ A: String, _ B: String) -> Bool {
 3         if A.length != B.length { return false }
 4         if A.count < 2 || A.count != B.count {
 5             return false
 6         }
 7         if A == B && Set(A).count < A.count {
 8             return true
 9         }
10         
11         var a = Array(A).map({ String($0) })
12         var b = Array(B).map({ String($0) })
13         var arr = [String]()
14         
15         if A != B {
16             for i in 0 ..< a.count {
17                 if a[i] != b[i] {
18                     arr.append(a[i])
19                     arr.append(b[i])
20                 }
21             }
22             if arr.reversed() == arr { return true }
23         }
24         
25         return false
26     }
27 }

40ms

 1 class Solution {
 2     func buddyStrings(_ A: String, _ B: String) -> Bool {
 3         guard A.count == B.count else {
 4             return false
 5         }
 6         var diff = [(Character, Character)]()
 7 
 8         for (a, b) in zip(A, B) {
 9             if a != b {
10                 diff.append((a, b))
11                 if diff.count > 2 {
12                     return false
13                 }
14             }
15         }
16 
17         return (diff.count == 0 && Set(Array(A)).count < A.count) ||
18                 (diff.count == 2 && diff[0] == (diff[1].1, diff[1].0))
19     }
20 }

 

转载于:https://www.cnblogs.com/strengthen/p/10588891.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/411958.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

三瞬属性matlab,matlab:out of memory 1

问题三&#xff1a;Increase the size of the swap file.wap space的设置与使用的操作系统有关&#xff0c;具体的如下&#xff1a;1.UNIXInformation about swap space can be procured by typing pstat -s at the UNIX command prompt. For detailed information on changing…

[导入]C#中TextBox只能输入数字的代码

文章来源:http://blog.csdn.net/21aspnet/archive/2007/03/20/1535640.aspx 转载于:https://www.cnblogs.com/zhaoxiaoyang2/archive/2007/03/21/816242.html

实验1c语言开发环境使用和数据类型、运算符和表达式

实验结论 由于这一次是第一次做实验有很多东西不熟悉 比如忘记加分号&#xff0c;用中文输入法打不对符号等等。总之经过这实验我学到了很多。#include <stdio.h> int main() {int x;printf("输入一个整数: \n");scanf("%d",&x);// 在处填写相应…

[vue] vue怎么改变插入模板的分隔符?

[vue] vue怎么改变插入模板的分隔符&#xff1f; optionMergeStrategies类型&#xff1a;{ [key: string]: Function }默认值&#xff1a;{}用法&#xff1a;Vue.config.optionMergeStrategies._my_option function (parent, child, vm) {return child 1}const Profile Vue…

php地址转换成经纬度,百度地图 获取地址转换为经纬度

html>根据地址查询经纬度a.{margin-right:100px;}style"position: absolute;margin-top:30px;width: 730px;height: 590px;top: 50px;border: 1px solid gray;overflow:hidden;">var map new BMap.Map("container");var point new BMap.Point(113.…

Nhibernate学习起步之many-to-one篇(转)

1. 学习目的: 通过进一步学习nhibernate基础知识&#xff0c;在实现单表CRUD的基础上&#xff0c;实现两表之间one-to-many的关系. 2. 开发环境必要准备 开发环境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition 必要准备: 学习上篇文章单…

[vue] 你了解什么是函数式组件吗?

[vue] 你了解什么是函数式组件吗&#xff1f; 函数式组件&#xff1a;需要提供一个render方法&#xff0c; 接受一个参数&#xff08;createElement函数&#xff09;&#xff0c; 方法内根据业务逻辑&#xff0c;通过createElement创建vnodes&#xff0c;最后return vnodescre…

列表元素的几种统计方法总结(嵌套列表)

&#xff08;1&#xff09;列表中的count方法(速度慢) #嵌套列表类型的统计 l [[1,2,3,4,5],[1,2,3,4,5],[5,6,7,8,9]] dictionary {} s set(l) for i in s:dict[i] l.count(i)&#xff08;2&#xff09;字典&#xff08;速度慢&#xff09; l [[1,2,3,4,5],[1,2,3,4,5],[5…

SQL Server数据库优化方案

SQL Server数据库优化方案 查询速度慢的原因很多&#xff0c;常见如下几种&#xff1a;1、没有索引或者没有用到索引(这是查询慢最常见的问题&#xff0c;是程序设计的缺陷)2、I/O吞吐量小&#xff0c;形成了瓶颈效应。3、没有创建计算列导致查询不优化。4、内存不足5、网络速度…

[vue] vue的:class和:style有几种表示方式?

[vue] vue的:class和:style有几种表示方式&#xff1f; :class 绑定变量 绑定对象 绑定一个数组 绑定三元表达式 :style 绑定变量 绑定对象 绑定函数返回值 绑定三元表达式个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷…

关于如何清除某个特定网站的缓存---基于Chrome浏览器

1、清除浏览器缓存 直接在浏览器设置里面清除浏览器的缓存会清除所有网站的缓存信息&#xff0c;这在某些时候是非常不方便的&#xff0c;毕竟不只有测试网站&#xff0c;还会有一些我们不想清除的信息也会被清除掉&#xff1b; 2、通过F12功能去清除浏览器缓存 转载于:https:/…

php中for循环流程图,PHP for循环

PHP for循环可以用来遍历一组指定的次数的代码。如果迭代次数已知&#xff0c;则应优先考虑使用for循环&#xff0c;否则使用while循环。for循环的语法for(initialization; condition; increment/decrement){ //code to be executed }for循环流程图示例代码-<?php for($n1;…

山西DotNet俱乐部网站改版成功

山西DotNet俱乐部改版成功网址为:http://www.dotnet.sx.cn或http://www.xy8.cn欢迎大家光临! 转载于:https://www.cnblogs.com/axzxs2001/archive/2007/04/05/700983.html

[vue] vue的is这个特性你有用过吗?主要用在哪些方面?

[vue] vue的is这个特性你有用过吗&#xff1f;主要用在哪些方面&#xff1f; vue中is的属性引入是为了解决dom结构中对放入html的元素有限制的问题<ul><li ismy-component></li> </ul>个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放…

Spring中AOP切面编程学习笔记

注解方式实现aop我们主要分为如下几个步骤&#xff1a;  1.在切面类&#xff08;为切点服务的类&#xff09;前用Aspect注释修饰&#xff0c;声明为一个切面类。  2.用Pointcut注释声明一个切点&#xff0c;目的是为了告诉切面&#xff0c;谁是它的服务对象。&#xff08;此…

Good Web

Good Web http://www.jxue.com/job/resume/ Englishhttp://www.jxue.com/zt/06zt/resume/http://www.cnrencai.com/ Jobposted on 2007-04-10 00:18 Steveson 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/Steveson/archive/2007/04/10/706450.h…

asp.net 生命周期中的时间流程

一、初始化 当页面被提交请求第一个方法永远是构造函数。您可以在构造函数里面初始一些自定义属性或对象&#xff0c;不过这时候因为页面还没有被完全初始化所以多少会有些限制。特别地&#xff0c;您需要使用HttpContext对象。当前可以使用的对象包括QueryString, Form以及Coo…

[vue] 怎么配置使vue2.0+支持TypeScript写法?

[vue] 怎么配置使vue2.0支持TypeScript写法&#xff1f; 配置ts-loader&#xff0c;tsconfig增加类型扩展&#xff0c;让ts识别vue文件vue文件中script里面换成ts写法&#xff0c; 需要增加几个ts扩展的package&#xff0c; 比如vue-property-decorator个人简介 我是歌谣&…

PHP迸发,PHP 开发 「十宗罪」

前言本文翻译自 10 Things Not To Do In PHP 7。全文列出了十条我们在 PHP7 开发中应注意避免的 反模式&#xff0c;觉得很有参考意义故翻译成中文供大家学习借鉴。1. 不要使用 mysql_ 函数在 PHP7 中&#xff0c;mysql_ 系列函数已经完全从核心代码中移除&#xff0c;你应该用…

元组、字典、集合的常用方法

一、元组类型 1、定义 t1 () print(t1, type(t1)) # 参数为for可以循环的对象(可迭代对象) t2 tuple("123") print(t2, type(t2)) t3 tuple([1, 2, 3]) print(t3, type(t3)) t4 tuple((7, 8, 9)) print(t4, type(t4)) # 思考:如何定义一个只有一个值的元组 # &qu…