Typorithm · easy

Valid Palindrome

valid-palindrometwo-pointersconvergingstringskip
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Valid Palindrome

0%

Two Pointers

Two pointers walk inward from both ends, skipping non-alphanumerics and comparing normalized characters.

A palindrome reads the same forward and backward, so compare characters from both ends inward.

Skip any character that is not a letter or digit before each comparison.

Lowercase both characters and compare; one mismatch means it is not a palindrome.

01

Pointers converge from the ends.

02

Skip non-alphanumeric characters.

03

Compare case-insensitively.

function isPalindrome(s) {
  const isAlphaNum = (ch) => /[a-z0-9]/i.test(ch);
  let left = 0;
  let right = s.length - 1;

  while (left < right) {
    while (left < right && !isAlphaNum(s[left])) {
      left += 1;
    }
    while (left < right && !isAlphaNum(s[right])) {
      right -= 1;
    }
    if (s[left].toLowerCase() !== s[right].toLowerCase()) {
      return false;
    }
    left += 1;
    right -= 1;
  }

  return true;
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Ends inward

Two pointers start at the ends and move toward each other.

let left = 0;
  let right = s.length - 1

2. Skip junk left

Advance past characters that are not letters or digits.

while (left < right && !isAlphaNum(s[left])) {
      left += 1;
    }

3. Case-insensitive compare

Mismatched normalized characters break the palindrome.

if (s[left].toLowerCase() !== s[right].toLowerCase()) {

4. Step inward

After a match, move both pointers one step in.

left += 1;
    right -= 1

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Valid Palindrome: Lesson & Practice · Typorithm | ApplyDjinn