Determine if a given string s is a palindrome after removing all characters that are not letters or digits and converting all uppercase letters to lowercase.
A resulting empty string should be considered a palindrome.
Function Signature
def is_palindrome(s: str) -> bool:
Rules
-
Letters are the English letters
a
-
z
and
A
-
Z
, and digits are
0
-
9
. Every other character (spaces, punctuation, symbols) is removed before the check.
-
An uppercase letter and its lowercase form count as the same character.
-
Return
True
if the cleaned string reads the same forward and backward, otherwise
False
.
Constraints
Examples
Input: s = "No lemon, no melon"
Output: True
After removing non-alphanumeric characters and converting uppercase letters to lowercase, the string becomes "nolemonnomelon", which reads the same forward and backward.
Input: s = "Hello, world!"
Output: False
After cleaning and converting, the string is "helloworld". It starts with h and ends with d, so it is not a palindrome.
Input: s = "?"
Output: True
The only character is removed, leaving the empty string "", which counts as a palindrome by definition.