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

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

GDScript: Advanced String Palindrome Checker

GDScript

Goal -- WPM

Ready
Exercise Algorithm Area
1func is_palindrome(text: String) -> bool:
2var cleaned_text = _normalize_and_clean_string(text)
3var n = cleaned_text.length()
4
5if n <= 1:
6return true
7
8var left = 0
9var right = n - 1
10
11while left < right:
12if cleaned_text[left] != cleaned_text[right]:
13return false
14left += 1
15right -= 1
16
17return true
18
19
20func _normalize_and_clean_string(text: String) -> String:
21var normalized = ""
22for char in text.to_lower():
23if char.is_alphanumeric():
24normalized += char
25return normalized
26
27
28# Example Usage:
29# var test_string1 = "A man, a plan, a canal: Panama"
30# var test_string2 = "race a car"
31# var test_string3 = ""
32# var test_string4 = "a"
33# var test_string5 = "hello"
34# print("Is '", test_string1, "' a palindrome? ", is_palindrome(test_string1))
35# print("Is '", test_string2, "' a palindrome? ", is_palindrome(test_string2))
36# print("Is '", test_string3, "' a palindrome? ", is_palindrome(test_string3))
37# print("Is '", test_string4, "' a palindrome? ", is_palindrome(test_string4))
38# print("Is '", test_string5, "' a palindrome? ", is_palindrome(test_string5))
Algorithm description viewbox

GDScript: Advanced String Palindrome Checker

Algorithm description:

This GDScript function determines if a given string is a palindrome, ignoring case, spaces, and punctuation. A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward. This is commonly used in text processing and natural language challenges.

Algorithm explanation:

The `is_palindrome` function first normalizes the input string by converting it to lowercase and removing non-alphanumeric characters using the `_normalize_and_clean_string` helper. This ensures that comparisons are case-insensitive and unaffected by irrelevant characters. The core logic then uses two pointers, `left` and `right`, initialized at the beginning and end of the cleaned string, respectively. 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 the function returns `false`. If the characters 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 the function returns `true`. Edge cases such as empty strings or single-character strings are handled by returning `true` immediately, as they are trivially palindromic. The time complexity is O(N), where N is the length of the input string, due to the linear scan for cleaning and the two-pointer comparison. The space complexity is also O(N) in the worst case for storing the cleaned string.

Pseudocode:

function is_palindrome(text):
    cleaned_text = normalize_and_clean(text)
    n = length of cleaned_text

    if n is less than or equal to 1:
        return true

    left = 0
    right = n - 1

    while left < right:
        if character at left in cleaned_text is not equal to character at right in cleaned_text:
            return false
        increment left
        decrement right

    return true

function normalize_and_clean(text):
    normalized_string = empty string
    for each character in lowercase version of text:
        if character is alphanumeric:
            append character to normalized_string
    return normalized_string