Last active
January 20, 2020 07:44
-
-
Save rudrankriyam/0a24ff4c2aecd074dbeefa2b1a67a38d to your computer and use it in GitHub Desktop.
Example for using the logical operators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func buddyStrings(_ A: String, _ B: String) -> Bool { | |
| if A.count != B.count { return false } | |
| var A = Array(A), B = Array(B) | |
| if A == B { | |
| return A.count > Set(A).count | |
| } else { | |
| return differentLetters(A, B) | |
| } | |
| } | |
| func differentLetters(_ A: [Character], _ B: [Character]) -> Bool { | |
| var indices = [Int]() | |
| for index in A.indices where A[index] != B[index] { | |
| indices.append(index) | |
| if indices.count > 2 { return false } | |
| } | |
| return (A[indices[0]] == B[indices[1]]) && (A[indices[1]] == B[indices[0]]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment