125/670

125. Valid Palindrome

Easy

Your function receives a string s that may contain letters, digits, spaces, and punctuation.

Judge it the way a human judges the phrase "never odd or even": ignore everything except letters and digits, treat uppercase and lowercase as the same, and check whether what remains reads identically forward and backward.

Return true if it does and false otherwise. A string that has no letters or digits at all counts as a palindrome — after filtering, there is nothing left to disagree.

Example 1:

Input: s = "No lemon, no melon!"

Output: true

Explanation: Keeping only letters and digits and lowercasing gives "nolemonnomelon", which reads the same in both directions.

Example 2:

Input: s = "open the door"

Output: false

Explanation: The filtered string is "openthedoor"; reversed it becomes "roodehtnepo", which is different.

Constraints:

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

Hints:

The simplest correct plan is two steps: build a cleaned copy (lowercased, letters and digits only), then compare it with its reverse.

To skip the extra copy, aim two indices at the ends of the original string. Slide each inward past non-alphanumeric characters, then compare the two characters case-insensitively; mismatch means false, meeting in the middle means true.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: s = "No lemon, no melon!"

Expected output: true