The Runtime Theory
easyleetcode#two-pointers#string

Valid Palindrome

Check if a string is a palindrome after stripping non-alphanumerics and case — two pointers from both ends meet in the middle.

The Runtime Theory Team1 min read
Solve it

solving happens on the judge — come back and mark it done

Sample cases

inA man, a plan, a canal: Panama

outtrue

inrace a car

outfalse

in

outtrue

Given a string, determine whether it is a palindrome after converting uppercase to lowercase and ignoring all non-alphanumeric characters. The machine must compare characters that may be separated by arbitrary punctuation, whitespace, and case differences.

The key insight: a palindrome reads the same from both ends, so two pointers — one from the left, one from the right — can compare inward. The trick is the skip: each pointer must advance past non-alphanumeric characters independently before comparing, because the ignored characters are not mirrored. Only compare when both pointers rest on real characters.

Approach:

  1. Put left at index 0 and right at the last index.
  2. Advance each pointer past non-alphanumerics; compare lowercased characters.
  3. Any mismatch returns False; pointers crossing means all pairs matched.
python
def is_palindrome(s):
    left, right = 0, len(s) - 1
    while left < right:
        while left < right and not s[left].isalnum():
            left += 1
        while left < right and not s[right].isalnum():
            right -= 1
        if s[left].lower() != s[right].lower():
            return False
        left, right = left + 1, right - 1
    return True

Time: O(n). Space: O(1) — no cleaned copy is built.

Trickiest edge case: all-ignored input like " " or ".," — the inner skip loops must be bounded by left < right or they run off the end of the string.

More practice in this topic

One dispatch a week

The trace behind each problem, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.