Skip to content

Commit

Permalink
Merge pull request #47 from nktssk/string-palindrome
Browse files Browse the repository at this point in the history
Add string palindrome algorithm
  • Loading branch information
carloshmartins authored May 14, 2024
2 parents 99e978b + 756cc8e commit fc999c3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
32 changes: 32 additions & 0 deletions algorithms/palindrome/palindrome_indices.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// A palindrome is a string that reads the same forwards and backwards.
//
// Examples: "level", "radar", "madam", "A man, a plan, a canal: Panama".

extension String {

/// Iteratively comparing characters from the beginning and end of the string. Only include letters and numbers.
/// - Complexity: O(n), without allocating new space.
func isPalindrome() -> Bool {
var leftIndex = startIndex
var rightIndex = index(before: endIndex)

while leftIndex < rightIndex {
guard self[leftIndex].isLetter || self[leftIndex].isNumber else {
leftIndex = index(after: leftIndex)
continue
}
guard self[rightIndex].isLetter || self[rightIndex].isNumber else {
rightIndex = index(before: rightIndex)
continue
}
guard self[leftIndex].lowercased() == self[rightIndex].lowercased() else {
return false
}

leftIndex = index(after: leftIndex)
rightIndex = index(before: rightIndex)
}

return true
}
}
44 changes: 44 additions & 0 deletions algorithms/palindrome/palindrome_recursion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// A palindrome is a string that reads the same forwards and backwards.
//
// Examples: "level", "radar", "madam", "A man, a plan, a canal: Panama".

extension String {

/// Recursively comparing characters from the beginning and end of the string. Only include letters and numbers.
/// - Complexity: O(n), without allocating new space.
func isPalindrome() -> Bool {
isPalindromeRecursion(
leftIndex: startIndex,
rightIndex: index(before: endIndex)
)
}

private func isPalindromeRecursion(
leftIndex: String.Index,
rightIndex: String.Index
) -> Bool {
guard leftIndex < rightIndex else {
return true
}
guard self[leftIndex].isLetter || self[leftIndex].isNumber else {
return isPalindromeRecursion(
leftIndex: index(after: leftIndex),
rightIndex: rightIndex
)
}
guard self[rightIndex].isLetter || self[rightIndex].isNumber else {
return isPalindromeRecursion(
leftIndex: leftIndex,
rightIndex: index(before: rightIndex)
)
}
guard self[leftIndex].lowercased() == self[rightIndex].lowercased() else {
return false
}

return isPalindromeRecursion(
leftIndex: index(after: leftIndex),
rightIndex: index(before: rightIndex)
)
}
}
13 changes: 13 additions & 0 deletions algorithms/palindrome/palindrome_reversed.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// A palindrome is a string that reads the same forwards and backwards.
//
// Examples: "level", "radar", "madam", "A man, a plan, a canal: Panama".

extension String {

/// Using the `reverse()` method to reverse the string and comparing it with the original. Only include letters and numbers.
/// - Complexity: O(n), with allocating O(n) space.
func isPalindrome() -> Bool {
let input = lowercased().filter { $0.isLetter || $0.isNumber }
return input == String(input.reversed())
}
}

0 comments on commit fc999c3

Please sign in to comment.