Skip to content

Instantly share code, notes, and snippets.

@mredig
Created May 8, 2019 16:19
Show Gist options
  • Save mredig/0ca9bc4bc4cdf8b4351093a618b50891 to your computer and use it in GitHub Desktop.
Save mredig/0ca9bc4bc4cdf8b4351093a618b50891 to your computer and use it in GitHub Desktop.
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