-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from nktssk/string-palindrome
Add string palindrome algorithm
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |