#125 Valid Palindrome

EasyTwo Pointers~10 min

A string counts as a palindrome here if, after dropping every character that isn't a letter or digit and lowercasing what remains, it reads the same forwards and backwards.

Given a string s containing printable ASCII, return true if it's a palindrome under that definition, false otherwise.

Examples

Inputs = "A man, a plan, a canal: Panama"
Outputtrue

Stripped and lowercased it becomes "amanaplanacanalpanama", which reads the same in both directions.

Inputs = "race a car"
Outputfalse

"raceacar" is not the same reversed ("racaecar").

Inputs = " "
Outputtrue

After filtering there's nothing left, and the empty string is a palindrome by convention.

Constraints

  • 1 ≤ s.length ≤ 2 × 10⁵
  • s consists of printable ASCII characters

The explainer is the good part.

Sign in for the full intuition build-up, every approach from naive to optimal with complexity analysis, progressive hints, and commented solutions in Python, JavaScript, and Java. All free.

Want a taste first? Try a free sample: Two Sum or Valid Parentheses.