← Back to DSA Animator
Valid Palindrome LC #125 Easy Two Pointers · Char Strip
Problem

A phrase is a palindrome if, after converting all uppercase letters to lowercase and removing all non-alphanumeric characters, it reads the same forward and backward. Given a string s, return true if it is a palindrome.

Example 1
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Constraints: 1 ≤ s.length ≤ 2×10⁵ | s consists only of printable ASCII characters
Try Examples
Custom:
Approach
Two pointers — skip junk, compare lowercase mirror chars
No cleaned string needed. Advance l/r past non-alphanumeric chars, then compare toLowerCase(s[l]) vs toLowerCase(s[r]). Any mismatch → false. O(n) time, O(1) space.
String Strip
l pointer
r pointer
skipped (non-alnum)
Cleaned:
Compare (lowercase)
s[l] → lower
s[r] → lower
Variables
l
0
r
s[l]
s[r]
match
Step Logic
Press ▶ Play or Next Step to begin the animation.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Init l = 0, r = n−1
2
While l < r: skip non-alphanumeric at l and r
3
Compare toLowerCase(s[l]) vs toLowerCase(s[r]) — mismatch → false
4
Match → l++, r--
5
When l ≥ r, all pairs matched → true
Time
O(n)
Space
O(1)
Why This Works

Two-pointer palindrome check without building a cleaned string. Non-alphanumeric characters are skipped in-place — they never participate in comparison. Only mirror positions matter; case is normalized via lowercase. Any mismatch immediately proves the string is not a palindrome; if pointers cross, every alphanumeric char had a matching partner.