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

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

Bicep String Palindrome Check

Bicep

Goal -- WPM

Ready
Exercise Algorithm Area
1param inputString string
2
3// Function to check if a string is a palindrome
4// A palindrome reads the same forwards and backwards.
5// This implementation ignores case and non-alphanumeric characters.
6func isPalindrome(str string) bool
7// Handle edge cases: empty string and single character string are palindromes
8if str == null || length(str) <= 1
9return true
10
11// Prepare the string: convert to lowercase and remove non-alphanumeric characters
12var cleanedString = ''
13for char in str
14if char >= 'a' && char <= 'z' || char >= '0' && char <= '9'
15set cleanedString += char
16else if char >= 'A' && char <= 'Z'
17// Convert uppercase to lowercase
18set cleanedString += toLower(char)
19
20// Handle edge case: if after cleaning, the string is empty or has one character
21if length(cleanedString) <= 1
22return true
23
24// Use two pointers, one from the start and one from the end
25var left = 0
26var right = length(cleanedString) - 1
27
28// Iterate while the left pointer is less than the right pointer
29while left < right
30// If characters at left and right pointers do not match, it's not a palindrome
31if cleanedString[left] != cleanedString[right]
32return false
33
34// Move pointers inwards
35set left = left + 1
36set right = right - 1
37
38// If the loop completes without returning false, the string is a palindrome
39return true
40
41// Helper function to convert a character to lowercase
42// This is a simplified version for demonstration; Bicep might have built-in functions
43func toLower(char string) string
44// Assuming ASCII for simplicity; a real implementation would be more robust
45if char >= 'A' && char <= 'Z'
46// Offset for ASCII lowercase conversion
47var asciiOffset = 32
48return string(int(char) + asciiOffset)
49else
50return char
51
52// Main expression to call the function and return the result
53output isStringPalindrome bool = isPalindrome(inputString)
Algorithm description viewbox

Bicep String Palindrome Check

Algorithm description:

This Bicep code defines a function to determine if a given string is a palindrome, ignoring case and non-alphanumeric characters. It preprocesses the input string by converting it to lowercase and removing irrelevant characters, then uses a two-pointer approach to efficiently compare characters from both ends of the cleaned string inwards. This is useful for validating user input, data sanitization, or in any scenario where checking for symmetrical text patterns is required.

Algorithm explanation:

The `isPalindrome` function first handles trivial edge cases: empty strings and single-character strings are considered palindromes. It then cleans the input string by converting all characters to lowercase and filtering out any character that is not an alphabet or a digit. This preprocessing ensures that comparisons are case-insensitive and unaffected by punctuation or spaces. A two-pointer approach is used: `left` starts at the beginning of the cleaned string, and `right` starts at the end. The loop continues as long as `left` is less than `right`. In each iteration, it compares the characters at the `left` and `right` pointers. If they differ, the string is not a palindrome, and `false` is returned. If they match, the pointers are moved inwards (`left` increments, `right` decrements). 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 the single pass for cleaning and the single pass for comparison. The space complexity is also O(N) in the worst case for storing the `cleanedString`.

Pseudocode:

Function isPalindrome(str):
  If str is null or length of str is 0 or 1:
    Return true.

  cleanedString = empty string.
  For each character in str:
    If character is alphanumeric:
      Append lowercase version of character to cleanedString.

  If length of cleanedString is 0 or 1:
    Return true.

  left = 0.
  right = length of cleanedString - 1.

  While left < right:
    If character at left != character at right:
      Return false.
    Increment left.
    Decrement right.

  Return true.