Skip to content

Add string palindrome algorithm #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
}