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

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

PowerShell: Check for Palindrome String

PowerShell

Goal -- WPM

Ready
Exercise Algorithm Area
1function Test-IsPalindrome {
2param(
3[string]$InputString
4)
5
6if ($InputString -eq $null -or $InputString.Length -le 1) {
7return $true
8}
9
10$left = 0
11$right = $InputString.Length - 1
12
13while ($left -lt $right) {
14if ($InputString[$left] -ne $InputString[$right]) {
15return $false
16}
17$left++
18$right--
19}
20
21return $true
22}
23
24function Get-StringLength {
25param(
26[string]$String
27)
28if ($String -eq $null) {
29return 0
30}
31return $String.Length
32}
33
34function Compare-Characters {
35param(
36[char]$Char1,
37[char]$Char2
38)
39return $Char1 -ne $Char2
40}
41
42function Decrement-Index {
43param(
44[int]$Index
45)
46return $Index - 1
47}
48
49function Increment-Index {
50param(
51[int]$Index
52)
53return $Index + 1
54}
55
56function Check-IndexBounds {
57param(
58[int]$Left,
59[int]$Right
60)
61return $Left -lt $Right
62}
Algorithm description viewbox

PowerShell: Check for Palindrome String

Algorithm description:

This function determines if a given string is a palindrome, meaning it reads the same forwards and backward. It compares characters from the beginning and end of the string, moving inwards. This is useful for text processing, data validation, and simple word puzzles.

Algorithm explanation:

The `Test-IsPalindrome` function efficiently checks if a string is a palindrome. It returns `true` immediately for null, empty, or single-character strings, as these are considered palindromes. For longer strings, it initializes two pointers: `$left` at the start (index 0) and `$right` at the end (index length - 1). A `while` loop continues as long as `$left` is less than `$right`. Inside the loop, it compares the characters at `$left` and `$right`. If they differ, the string is not a palindrome, and `false` is returned. If they match, `$left` is incremented and `$right` is decremented to move the pointers inwards. If the loop completes without finding mismatches, the string is a palindrome, and `true` is returned. The time complexity is O(n/2), which simplifies to O(n), as it traverses at most half the string. The space complexity is O(1) as it uses a constant amount of extra space. Helper functions `Get-StringLength`, `Compare-Characters`, `Decrement-Index`, `Increment-Index`, and `Check-IndexBounds` enhance modularity and readability.

Pseudocode:

FUNCTION Test-IsPalindrome(InputString):
  IF InputString is null OR length of InputString <= 1:
    RETURN true
  SET left = 0
  SET right = length of InputString - 1
  WHILE left < right:
    IF character at left != character at right:
      RETURN false
    INCREMENT left
    DECREMENT right
  RETURN true
END FUNCTION

FUNCTION Get-StringLength(String):
  IF String is null:
    RETURN 0
  RETURN length of String
END FUNCTION

FUNCTION Compare-Characters(Char1, Char2):
  RETURN Char1 != Char2
END FUNCTION

FUNCTION Decrement-Index(Index):
  RETURN Index - 1
END FUNCTION

FUNCTION Increment-Index(Index):
  RETURN Index + 1
END FUNCTION

FUNCTION Check-IndexBounds(Left, Right):
  RETURN Left < Right
END FUNCTION