F# String Palindrome Checker with Edge Cases
(* Checks if a given string is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, ignoring spaces, punctuation, and capitalization. *) let isPalindrome (input: string) : bool = // Handle edge case: empty...
This F# function determines if a given string is a palindrome. It first normalizes the input by removing non-alphanumeric characters and converting it to lowercase. Then, it uses a recursive helper function to compare ch...
The `isPalindrome` function first handles edge cases: an empty string and a single-character string are both considered palindromes. It then normalizes the input string by removing all non-alphanumeric characters and converting it to lowercase using regular expressions and the `ToLower()` method. The core logic is delegated to the `checkPalindromeRecursive` helper function. This function employs a two-pointer approach, with `left` starting at the beginning and `right` at the end of the normalized string. In each recursive call, it checks if the characters at `left` and `right` are equal. If they are not, the string is not a palindrome, and `false` is returned. If `left` becomes greater than or equal to `right`, it means all corresponding characters have matched, and the string is a palindrome, returning `true`. The time complexity is O(N) due to string normalization and traversal, where N is the length of the input string. The space complexity is O(N) in the worst case for the normalized string and the recursion stack.
function isPalindrome(input): if input is empty or null: return true normalize input: remove non-alphanumeric, convert to lowercase if normalized length is 0 or 1: return true call recursive helper checkPalindromeRecursi...