Skip to content

visual walkthrough

Valid Palindrome

EasyOpposite EndsReported at: MetaAmazonMicrosoft+3

The simplest opposite-ends scan, with character filtering.

Solve on LeetCode

The idea

A palindrome reads the same from both ends, so you only ever need to compare the outermost characters, then move inward. Two pointers do that without building a cleaned-up copy.

Characters that aren't letters or digits are simply stepped over when a pointer lands on them.

Complexity

approachtimespace
Clean and reverseO(n)O(n)
Two pointersO(n)O(1)

More walkthroughs