Determine whether a string is a palindrome after ignoring non-alphanumeric characters and case.
Given a string, decide whether it reads the same forward and backward after:
- ignoring all characters that are not letters or digits, and
- treating uppercase and lowercase letters as equal.
Return true if the cleaned string is a palindrome; otherwise return false.
A palindrome is a sequence that matches its reverse.
Input Format
- A single string
s. - The string may contain letters, digits, spaces, punctuation, and other visible characters.
Output Format
- Return a boolean value:
trueifsis a valid palindrome under the rules abovefalseotherwise
Constraints
0 <= s.length- The string length is small to moderate for interview-style checking.
- You should solve it without building unnecessary extra structures when possible.
Example 1
Input
s = "A man, a plan, a canal: Panama"
Output
true
Explanation
After removing punctuation and spaces and converting to lowercase, the string becomes amanaplanacanalpanama, which is the same forward and backward.
Example 2
Input
s = "race a car"
Output
false
Explanation
The cleaned string is raceacar, which does not match its reverse.
Show 1 more example
Example 3
Input
s = " "
Output
true
Explanation
There are no alphanumeric characters, so the cleaned string is empty and is considered a palindrome.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.