codebrew.ai

Valid Palindrome

Converge from both ends, skipping non-alphanumerics.

Approach

Put one pointer at each end. Advance past any non-alphanumeric character, compare the two (case-insensitively), and move inward. If any pair differs it's not a palindrome. This avoids building a cleaned copy of the string.

Time complexity

O(n)

Space complexity

O(1)

Common mistake

Forgetting to skip punctuation/spaces on both pointers, or comparing without normalizing case.

See it run, step by step

Generate an interactive lesson for Valid Palindrome — trace every variable and watch the algorithm execute until it clicks.

Start a lesson on Valid Palindrome

Related problems