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

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

PowerShell Palindrome Checker

PowerShell

Goal -- WPM

Ready
Exercise Algorithm Area
1function Is-Palindrome {
2param(
3[Parameter(Mandatory=$true)]
4[string]$InputString
5)
6
7$normalizedString = _NormalizeString($InputString);
8
9if ([string]::IsNullOrEmpty($normalizedString)) {
10return $true; # Empty string is considered a palindrome
11}
12
13$left = 0;
14$right = $normalizedString.Length - 1;
15
16while ($left -lt $right) {
17if ($normalizedString[$left] -ne $normalizedString[$right]) {
18return $false;
19}
20$left++;
21$right--;
22}
23
24return $true;
25}
26
27function _NormalizeString {
28param(
29[string]$StringValue
30)
31
32# Remove non-alphanumeric characters and convert to lowercase
33$cleanedString = [regex]::Replace($StringValue, '[^a-zA-Z0-9]', '').ToLower();
34return $cleanedString;
35}
Algorithm description viewbox

PowerShell Palindrome Checker

Algorithm description:

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 helper function `_NormalizeString`. Then, it compares characters from both ends of the normalized string inwards. This is useful for validating user input, in text processing, or as a basic string algorithm exercise.

Algorithm explanation:

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.

Pseudocode:

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 pointer to length of normalizedString - 1

  WHILE left < right DO
    IF character at normalizedString[left] is NOT equal to character at normalizedString[right] THEN
      RETURN false
    END IF
    Increment left
    Decrement right
  END WHILE

  RETURN true
END FUNCTION

FUNCTION _NormalizeString(StringValue):
  Remove all non-alphanumeric characters from StringValue
  Convert the result to lowercase
  RETURN the processed string
END FUNCTION