PowerShell Palindrome Checker
function Is-Palindrome { param( [Parameter(Mandatory=$true)] [string]$InputString ) $normalizedString = _NormalizeString($InputString); if ([string]::IsNullOrEmpty($normalizedString)) { return $true; # Empty string is considered a palindrome } $left = 0; $right = $normalizedStrin...
This PowerShell script defines `Is-Palindrome`, a function that determines if a string is a palindrome. It first normalizes the input string by removing non-alphanumeric characters and converting it to lowercase using a...
The `Is-Palindrome` function takes a string and first calls `_NormalizeString` to prepare it for comparison. The `_NormalizeString` helper uses a regular expression `[^a-zA-Z0-9]` to find and remove any characters that are not letters or numbers, and then converts the result to lowercase using `.ToLower()`. This ensures that case and punctuation do not affect the palindrome check. If the normalized string is empty (meaning the original string was empty or contained only non-alphanumeric characters), it's considered a palindrome and `true` is returned. Otherwise, two pointers, `left` and `right`, are initialized to the start and end of the normalized string, respectively. A `while` loop continues as long as `left` is less than `right`. Inside the loop, it compares the characters at the `left` and `right` pointers. If they are not equal, the string is not a palindrome, and `false` is returned immediately. If the characters match, `left` is incremented and `right` is decremented to move the pointers inward. If the loop completes without finding any mismatches, it means the string is a palindrome, and `true` is returned. The time complexity is O(n), where n is the length of the input string, due to normalization and the single pass for comparison. The space complexity is O(n) in the worst case for storing the normalized string.
FUNCTION Is-Palindrome(InputString): Call _NormalizeString(InputString) and store result in normalizedString IF normalizedString is null or empty THEN RETURN true END IF Initialize left pointer to 0 Initialize right poin...