noodleProblems/
Valid Palindrome
#12

Valid Palindrome

AlgorithmeasyTwo PointersString

Given a string s, return true if it reads the same forwards and backwards after lowercasing and removing every non-alphanumeric character, and false otherwise.

An empty string (after cleaning) counts as a palindrome.

Example cases

  • classic
    in s = "A man, a plan, a canal: Panama"
    out true
    Cleaned to "amanaplanacanalpanama", a palindrome.
  • not a palindrome
    in s = "race a car"
    out false
  • empty after cleaning
    in s = " "
    out true
  • alphanumeric mix
    in s = "0P"
    out false

Constraints

  • 1 <= s.length <= 2 * 10^5
  • s consists only of printable ASCII characters.
Saved
s =
"A man, a plan, a canal: Panama"