Valid Alphanumeric Palindrome
In text processing and data validation, efficiently checking for palindromes is a common task. This problem challenges you to determine if a given string is a palindrome, but with a twist: you must consider only alphanumeric characters and ignore case. This is crucial for normalizing user input or text data before analysis, ensuring that the core content is evaluated regardless of formatting or capitalization.
Implement a function isPalindrome that takes a string s and returns true if it is an alphanumeric palindrome, and false otherwise. Your implementation must satisfy the following:
a-z, A-Z, and digits 0-9). All other characters (e.g., spaces, punctuation, symbols) should be ignored.s (string)
true if s is an alphanumeric palindrome, false otherwise."" is considered a valid palindrome.s contains no alphanumeric characters after filtering, it should also be considered a valid palindrome (e.g., ".," becomes an empty string, which is a palindrome).Examples
Example 1
Input:
s = "A man, a plan, a canal: Panama"
Output:
true
Note:
After converting to lowercase and removing non-alphanumeric characters, it becomes "amanaplanacanalpanama", which is a palindrome.
Example 2
Input:
s = "race a car"
Output:
false
Note:
After converting to lowercase and removing non-alphanumeric characters, it becomes "raceacar", which is not a palindrome.
Example 3
Input:
s = " "
Output:
true
Note:
After removing non-alphanumeric characters, the string becomes empty, which is considered a palindrome.
Example 4
Input:
s = "Madam"
Output:
true
Note:
After converting to lowercase, it becomes "madam", which is a palindrome.
Constraints
0 <= s.length <= 2 * 10^5s consists only of printable ASCII characters.Loading editor...