Created
November 6, 2017 20:30
-
-
Save volkanbicer/c216877508d0af49922d36b59f81ca9a to your computer and use it in GitHub Desktop.
This file contains 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
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