Skip to content

Instantly share code, notes, and snippets.

@rudrankriyam
Last active January 20, 2020 07:44
Show Gist options
  • Select an option

  • Save rudrankriyam/0a24ff4c2aecd074dbeefa2b1a67a38d to your computer and use it in GitHub Desktop.

Select an option

Save rudrankriyam/0a24ff4c2aecd074dbeefa2b1a67a38d to your computer and use it in GitHub Desktop.
Example for using the logical operators
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