forked from duliodenis/swift-coding-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_palindrome.swift
50 lines (39 loc) · 1.63 KB
/
02_palindrome.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Is a string a palindrome?
// Write a function that accepts a String as its only parameter, and returns
// true if the string reads the same when reversed, ignoring case.
func isPalindrome(_ input: String) -> Bool {
let reversedInput = String(input.characters.reversed())
if input == reversedInput {
return true
}
return false
}
// the one line of code solution
func isPalindrome2(_ input: String) -> Bool {
return String(input.characters.reversed()) == input
}
// case insensitive
func isPalindromeCaseInsensitive(_ input: String) -> Bool {
return String(input.lowercased().characters.reversed()) == input.lowercased()
}
print(isPalindrome("rotator")) // true
print(isPalindrome("palindrome")) // false
print(isPalindrome2("rotator")) // true
print(isPalindrome2("roTator")) // false - not case sensitive
print(isPalindrome2("palindrome")) // false
print(isPalindromeCaseInsensitive("rotator")) // true
print(isPalindromeCaseInsensitive("roTator")) // true - case sensitive
print(isPalindromeCaseInsensitive("palindrome")) // false
// Swift 4
func isPalindrome3(_ word: String) -> Bool {
return word == String(word.reversed())
}
func isPalindromeCaseInsensitive2(_ word:String) -> Bool {
return word.lowercased() == String(word.reversed()).lowercased()
}
print(isPalindrome3("rotator")) // true
print(isPalindrome3("roTator")) // false - not case sensitive
print(isPalindrome3("palindrome")) // false
print(isPalindromeCaseInsensitive2("rotator")) // true
print(isPalindromeCaseInsensitive2("roTator")) // true - case sensitive
print(isPalindromeCaseInsensitive2("palindrome")) // false