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

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

T-SQL: Check for Palindrome String

T-SQL

Goal -- WPM

Ready
Exercise Algorithm Area
1CREATE FUNCTION dbo.IsPalindrome (@InputString NVARCHAR(MAX))
2RETURNS BIT
3AS
4BEGIN
5DECLARE @CleanedString NVARCHAR(MAX) = '';
6DECLARE @i INT = 1;
7DECLARE @Len INT = LEN(@InputString);
8DECLARE @CurrentChar NCHAR(1);
9
10-- Clean the string: remove non-alphanumeric and convert to lowercase
11WHILE @i <= @Len
12BEGIN
13SET @CurrentChar = SUBSTRING(@InputString, @i, 1);
14-- Check if character is alphanumeric
15IF (@CurrentChar LIKE '[a-z,A-Z,0-9]')
16BEGIN
17SET @CleanedString = @CleanedString + LOWER(@CurrentChar);
18END
19SET @i = @i + 1;
20END
21
22DECLARE @CleanedLen INT = LEN(@CleanedString);
23
24-- Edge cases: empty or single-character strings are palindromes
25IF @CleanedLen <= 1
26BEGIN
27RETURN 1;
28END
29
30DECLARE @Left INT = 1;
31DECLARE @Right INT = @CleanedLen;
32
33-- Compare characters from both ends inwards
34WHILE @Left < @Right
35BEGIN
36IF SUBSTRING(@CleanedString, @Left, 1) <> SUBSTRING(@CleanedString, @Right, 1)
37BEGIN
38RETURN 0; -- Not a palindrome
39END
40SET @Left = @Left + 1;
41SET @Right = @Right - 1;
42END
43
44RETURN 1; -- It's a palindrome
45END
Algorithm description viewbox

T-SQL: Check for Palindrome String

Algorithm description:

This T-SQL function checks if a given string is a palindrome. It first cleans the input string by removing non-alphanumeric characters and converting it to lowercase to ensure a case-insensitive and character-agnostic comparison. It then compares characters from the beginning and end of the cleaned string, moving inwards. If any pair of characters does not match, the string is not a palindrome. Empty strings and single-character strings are considered palindromes. This is useful for text processing, data validation, and recreational programming challenges.

Algorithm explanation:

The function preprocesses the input string by iterating through it, keeping only alphanumeric characters and converting them to lowercase. This ensures that 'Racecar' and 'race car' are treated the same. It handles edge cases where the cleaned string is empty or has only one character, returning true as these are palindromes. The core logic uses two pointers, `Left` starting at the beginning and `Right` at the end of the cleaned string. In a loop, it compares the characters at `Left` and `Right`. If they differ, the function returns false. If they match, `Left` is incremented and `Right` is decremented. The loop continues until `Left` is no longer less than `Right`. If the loop completes without returning false, it means all corresponding characters matched, and the function returns true. The time complexity is O(N) for cleaning the string and O(N/2) for comparison, resulting in O(N) overall, where N is the length of the original string. The space complexity is O(N) for storing the cleaned string. The invariant is that all characters outside the `[Left, Right]` range have been successfully matched.

Pseudocode:

FUNCTION IsPalindrome(inputString):
  cleanedString = ""
  FOR each character in inputString:
    IF character is alphanumeric THEN
      cleanedString = cleanedString + toLower(character)

  n = length(cleanedString)
  IF n <= 1 THEN RETURN TRUE

  left = 0
  right = n - 1

  WHILE left < right:
    IF cleanedString[left] != cleanedString[right] THEN RETURN FALSE
    left = left + 1
    right = right - 1

  RETURN TRUE