1. 程式人生 > >[Swift]LeetCode859. 親密字符串 | Buddy Strings

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

ngs sem 小寫 我們 The reverse efault true let

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.

給定兩個由小寫字母構成的字符串 AB ,只要我們可以通過交換 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. AB 僅由小寫字母構成。

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 }

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