Skip to content
Shubham JangleResources
Arrays & strings

Explore topics

Valid Palindrome

Watch the walkthrough
Open on Instagram

Problem

Ignore non-alphanumeric characters and case, then decide whether the remaining characters form a palindrome.

Approach and revision checks

Two pointers skip punctuation and compare opposite characters: O(n) time and O(1) auxiliary space. The copied helper recognizes ASCII letters and digits only; it does not provide full Unicode normalization. Test empty input, punctuation-only input, mixed case, and a non-palindrome. Example: "A man, a plan, a canal: Panama" -> true.

Original repository source

Copied from the supplied local DSA-Series checkout; code and comments are preserved. Run alternative definitions separately when comparing approaches.

6-valid-palindrome.js

Original file on GitHub

function isAlnum(char) {
  const code = char.charCodeAt(0);
  return (
    (code >= 48 && code <= 57) || // 0-9
    (code >= 65 && code <= 90) || // A-Z
    (code >= 97 && code <= 122) // a-z
  );
}

var isPalindrome = function (s) {
  let start = 0;
  let end = s.length - 1;

  while (start <= end) {
    while (start < end && !isAlnum(s[start])) {
      start++;
    }

    while (end > start && !isAlnum(s[end])) {
      end--;
    }

    if (s[start].toLowerCase() !== s[end].toLowerCase()) {
      return false;
    }

    start++;
    end--;
  }

  return true;
};