Created
May 27, 2018 18:36
-
-
Save BrianLitwin/d789451a8671d517415c9c5bf7ed0a91 to your computer and use it in GitHub Desktop.
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 oneAway(s1: String, s2: String) -> Bool { | |
guard abs(s1.count - s2.count) <= 1 else { return false } | |
var foundDifference = false | |
var index1 = 0 | |
var index2 = 0 | |
while index1 < s1.count && index2 < s2.count { | |
let c1 = s1[s1.index(s1.startIndex, offsetBy: index1)] | |
let c2 = s2[s2.index(s1.startIndex, offsetBy: index2)] | |
if c1 != c2 { | |
if foundDifference { return false } | |
foundDifference = true | |
if index1 != index2 { | |
return false | |
} | |
if s1.count > s2.count { | |
index1 += 1 | |
continue | |
} else if s2.count > s1.count { | |
index2 += 1 | |
continue | |
} | |
} | |
index1 += 1 | |
index2 += 1 | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment