|
| 1 | +// A palindrome is a string that reads the same forwards and backwards. |
| 2 | +// |
| 3 | +// Examples: "level", "radar", "madam", "A man, a plan, a canal: Panama". |
| 4 | + |
| 5 | +struct PalindromeSolutionFirst { |
| 6 | + |
| 7 | + /// Using the `reverse()` method to reverse the string and comparing it with the original. Only include letters and numbers. |
| 8 | + /// - Complexity: O(n), with allocating O(n) space. |
| 9 | + func isPalindrome(_ input: String) -> Bool { |
| 10 | + let input = input.lowercased().filter { $0.isLetter || $0.isNumber } |
| 11 | + return input == String(input.reversed()) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +struct PalindromeSolutionSecond { |
| 16 | + |
| 17 | + /// Iteratively comparing characters from the beginning and end of the string. Only include letters and numbers. |
| 18 | + /// - Complexity: O(n), without allocating new space. |
| 19 | + func isPalindrome(_ input: String) -> Bool { |
| 20 | + var leftIndex = input.startIndex |
| 21 | + var rightIndex = input.index(before: input.endIndex) |
| 22 | + |
| 23 | + while leftIndex < rightIndex { |
| 24 | + if !input[leftIndex].isLetter && !input[leftIndex].isNumber { |
| 25 | + leftIndex = input.index(after: leftIndex) |
| 26 | + continue |
| 27 | + } |
| 28 | + if !input[rightIndex].isLetter && !input[rightIndex].isNumber { |
| 29 | + rightIndex = input.index(before: rightIndex) |
| 30 | + continue |
| 31 | + } |
| 32 | + if input[leftIndex].lowercased() != input[rightIndex].lowercased() { |
| 33 | + return false |
| 34 | + } |
| 35 | + |
| 36 | + leftIndex = input.index(after: leftIndex) |
| 37 | + rightIndex = input.index(before: rightIndex) |
| 38 | + } |
| 39 | + |
| 40 | + return true |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +struct PalindromeSolutionThird { |
| 45 | + |
| 46 | + /// Recursively comparing characters from the beginning and end of the string. Only include letters and numbers. |
| 47 | + /// - Complexity: O(n), without allocating new space. |
| 48 | + func isPalindrome(_ input: String) -> Bool { |
| 49 | + isPalindromeRecursion( |
| 50 | + input, |
| 51 | + leftIndex: input.startIndex, |
| 52 | + rightIndex: input.index(before: input.endIndex) |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + private func isPalindromeRecursion( |
| 57 | + _ input: String, |
| 58 | + leftIndex: String.Index, |
| 59 | + rightIndex: String.Index |
| 60 | + ) -> Bool { |
| 61 | + if leftIndex >= rightIndex { |
| 62 | + return true |
| 63 | + } |
| 64 | + if !input[leftIndex].isLetter && !input[leftIndex].isNumber { |
| 65 | + return isPalindromeRecursion( |
| 66 | + input, |
| 67 | + leftIndex: input.index(after: leftIndex), |
| 68 | + rightIndex: rightIndex |
| 69 | + ) |
| 70 | + } |
| 71 | + if !input[rightIndex].isLetter && !input[rightIndex].isNumber { |
| 72 | + return isPalindromeRecursion( |
| 73 | + input, |
| 74 | + leftIndex: leftIndex, |
| 75 | + rightIndex: input.index(before: rightIndex) |
| 76 | + ) |
| 77 | + } |
| 78 | + if input[leftIndex].lowercased() != input[rightIndex].lowercased() { |
| 79 | + return false |
| 80 | + } |
| 81 | + |
| 82 | + return isPalindromeRecursion( |
| 83 | + input, |
| 84 | + leftIndex: input.index(after: leftIndex), |
| 85 | + rightIndex: input.index(before: rightIndex) |
| 86 | + ) |
| 87 | + } |
| 88 | +} |
0 commit comments