ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

F# String Palindrome Checker with Edge Cases

F#

Goal -- WPM

Ready
Exercise Algorithm Area
1(*
2Checks if a given string is a palindrome.
3A palindrome is a word, phrase, number, or other sequence of characters
4which reads the same backward as forward, ignoring spaces, punctuation, and capitalization.
5*)
6let isPalindrome (input: string) : bool =
7// Handle edge case: empty string is considered a palindrome
8if String.IsNullOrEmpty input then
9true
10else
11// Normalize the string: remove non-alphanumeric characters and convert to lowercase
12let normalized = System.Text.RegularExpressions.Regex.Replace(input, "[^a-zA-Z0-9]", "").ToLower()
13
14// Handle edge case: single character string is a palindrome
15if normalized.Length <= 1 then
16true
17else
18// Use a helper function to perform the actual palindrome check
19checkPalindromeRecursive normalized 0 (normalized.Length - 1)
20
21
22(*
23Recursive helper function to check if a substring is a palindrome.
24Compares characters from both ends moving inwards.
25*)
26let rec checkPalindromeRecursive (str: string) (left: int) (right: int) : bool =
27// Base case: if left pointer crosses or meets right pointer, the string is a palindrome
28if left >= right then
29true
30// If characters at left and right pointers do not match, it's not a palindrome
31elif str.[left] <> str.[right] then
32false
33// Recursive step: move pointers inwards and continue checking
34else
35checkPalindromeRecursive str (left + 1) (right - 1)
36
37
38// Example Usage:
39let test1 = "A man, a plan, a canal: Panama"
40let test2 = "race a car"
41let test3 = ""
42let test4 = "a"
43let test5 = "Madam"
44
45printfn "%s is a palindrome: %b" test1 (isPalindrome test1)
46printfn "%s is a palindrome: %b" test2 (isPalindrome test2)
47printfn "%s is a palindrome: %b" test3 (isPalindrome test3)
48printfn "%s is a palindrome: %b" test4 (isPalindrome test4)
49printfn "%s is a palindrome: %b" test5 (isPalindrome test5)
Algorithm description viewbox

F# String Palindrome Checker with Edge Cases

Algorithm description:

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 characters from the beginning and end of the normalized string, moving inwards. This is useful for validating user input, such as passwords or search queries, where case and punctuation should be ignored.

Algorithm explanation:

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.

Pseudocode:

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 checkPalindromeRecursive(normalized, 0, normalized.length - 1)

function checkPalindromeRecursive(str, left, right):
  if left >= right:
    return true
  if str[left] is not equal to str[right]:
    return false
  return checkPalindromeRecursive(str, left + 1, right - 1)