SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Algorithm Patterns OverviewFrequency CountersTwo SumValid AnagramGroup AnagramsFrequency Counters and Hash-Map LookupTwo PointersValid PalindromeTwo Sum IIContainer With Most WaterTwo-Pointer ReasoningSliding WindowMaximum Average Subarray ILongest Substring Without Repeating CharactersMinimum Size Subarray SumSliding-Window Reasoning
Algorithm Patterns

Valid Palindrome

Apply the two pointers pattern to solve Valid Palindrome, run test cases, and review the solution when needed.

Try It Yourself

Use the editor below to decide whether the string is a palindrome.

Compare letters and digits from both ends

Start one pointer at each end. Skip characters other than letters and digits, ignore capitalization when comparing letters, and return false on the first mismatch. Check single-character strings too.

Submit your implementation when it passes the examples.

Valid Palindrome

easyTwo Pointers · Strings20 minLC 125
Problem

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing every non-alphanumeric character, it reads the same forward and backward.

Given a string s, return true when it is a palindrome and false otherwise.

Example 1:

Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: Removing spaces and punctuation and using lowercase gives amanaplanacanalpanama, which reads the same in both directions.

Example 2:

Input: s = "race a car"
Output: false
Explanation: Removing spaces gives raceacar. Its middle characters are e and a, which do not match.

Constraints

  • 1 ≤ s.length ≤ 200,000
  • `s` contains printable ASCII characters.
0 attempts

Test results

Results appear here after a run.

Solution

Skipping the punctuation in A,b!b?A leaves the mirrored pairs a/a and b/b.

The two-pointers approach compares the next meaningful character from each end of the string. The left pointer moves forward and the right pointer moves backward. Before comparing characters, each pointer skips anything that is not a letter or digit.

The normalized row in the figure shows which characters count. The implementation walks the original printable ASCII string and skips punctuation in place. When both pointers reach valid characters, compare their lowercase forms. A mismatch means the string cannot be a palindrome. If they match, move both pointers inward and repeat until they meet.

function isAlphaNumeric(character) {
  const code = character.charCodeAt(0);

  const isDigit = code >= 48 && code <= 57;
  const isUppercaseLetter = code >= 65 && code <= 90;
  const isLowercaseLetter = code >= 97 && code <= 122;

  return isDigit || isUppercaseLetter || isLowercaseLetter;
}

function isPalindrome(s) {
  let left = 0;
  let right = s.length - 1;

  while (left < right) {
    while (left < right && !isAlphaNumeric(s[left])) {
      left++;
    }

    while (left < right && !isAlphaNumeric(s[right])) {
      right--;
    }

    if (s[left].toLowerCase() !== s[right].toLowerCase()) {
      return false;
    }

    left++;
    right--;
  }

  return true;
}

Big O notation

MeasureComplexityExplanation
TimeO(n)Each pointer moves across the string at most once.
Auxiliary spaceO(1)The algorithm uses only two pointer indices and a fixed number of local variables.

How is this practice?

Previous lessonTwo PointersNext lessonTwo Sum II
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation