为有牺牲多壮志,敢教日月换新天。

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

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 

posted @ 2019-03-24 16:49  为敢技术  阅读(364)  评论(0编辑  收藏  举报