Skip to content

Instantly share code, notes, and snippets.

@volkanbicer
Created November 6, 2017 20:30
Show Gist options
  • Save volkanbicer/c216877508d0af49922d36b59f81ca9a to your computer and use it in GitHub Desktop.
Save volkanbicer/c216877508d0af49922d36b59f81ca9a to your computer and use it in GitHub Desktop.
import Foundation
func palindrom(_ string: String) -> Bool{
let strippedString = string.replacingOccurrences(of: "\\W", with: "", options: .regularExpression, range: nil)
let length = strippedString.characters.count
if length == 0 {
return true
}else if length > 1{
return isPalindrom(strippedString.lowercased(), left: 0, right: length-1)
}else{
return false
}
}
private func isPalindrom(_ string: String, left:Int, right:Int) -> Bool{
if left >= right{
return true
}
let lhs = string[string.index(string.startIndex, offsetBy: left)]
let rhs = string[string.index(string.startIndex, offsetBy: right)]
if lhs != rhs{
return false
}
return isPalindrom(string, left: left+1, right: right-1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment