Created
May 8, 2019 16:19
-
-
Save mredig/0ca9bc4bc4cdf8b4351093a618b50891 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 | |
extension String { | |
func isAPalindrome() -> Bool { | |
var string = self.lowercased() | |
let characterSet = Set("abcdefghijklmnopqrstuvwxyz") | |
string = string.filter { characterSet.contains($0) } | |
return string == String(string.reversed()) | |
} | |
func anotherIsAPalindrome() -> Bool { | |
var string = self.lowercased() | |
let characterSet = Set("abcdefghijklmnopqrstuvwxyz") | |
string = string.filter { characterSet.contains($0) } | |
let strArray = Array(string) | |
for (index, letter) in strArray.enumerated() { | |
if letter != strArray[strArray.count - 1 - index] { | |
return false | |
} | |
if index > strArray.count / 2 { | |
break | |
} | |
} | |
return true | |
} | |
} | |
"tacocat".isAPalindrome() | |
"never odd or even".isAPalindrome() | |
"race car".isAPalindrome() | |
"daily".isAPalindrome() | |
"tacocat".anotherIsAPalindrome() | |
"never odd or even".anotherIsAPalindrome() | |
"race car".anotherIsAPalindrome() | |
"daily".anotherIsAPalindrome() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment