Valid Palindrome
EasyTwo Pointers
Given a string s, decide whether it is a palindrome after cleaning it up:
- keep only alphanumeric characters (letters
a-z,A-Zand digits0-9) and drop everything else, including spaces, punctuation and underscores; - treat uppercase and lowercase letters as equal.
Return True if the cleaned string reads the same forward and backward, otherwise False. A string that is empty after cleaning counts as a palindrome.
Example 1
- Input:
- s = "No lemon, no melon"
- Output:
- True
- Explanation
- Stripping the space and the comma and lowercasing leaves
nolemonnomelon, which reads the same both ways.
Example 2
- Input:
- s = "...,,,!!!"
- Output:
- True
- Explanation
- Every character is stripped, and an empty string reads the same both ways.
Example 3
- Input:
- s = "1a2"
- Output:
- False
- Explanation
- Digits are alphanumeric, so they are kept, and
1a2reversed is2a1.
Constraints
1 <= s.length <= 2 * 10^5sconsists of printable ASCII characters