Validate Alphanumeric Palindrome
At Salesforce, ensuring data quality and user experience is paramount. When dealing with user-generated content, display names, or records, we often need to validate and normalize text. One common task is checking for palindromic properties, especially when certain characters or casing should be ignored. This problem simulates such a scenario, requiring you to implement a core utility function that can be leveraged in data validation pipelines or UI components.
Implement a function isPalindrome that determines if a given string s is a palindrome. For the purpose of this problem, a string is considered a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forwards and backwards.
s - A string representing the input text.true if s is an alphanumeric palindrome, false otherwise."" is considered a valid palindrome."!@#$") should be treated as an empty string for palindrome check and thus return true.Examples
Example 1
Input:
s = "A man, a plan, a canal: Panama"
Output:
true
Note:
After filtering and converting to lowercase, the string becomes "amanaplanacanalpanama", which is a palindrome.
Example 2
Input:
s = "race a car"
Output:
false
Note:
After filtering and converting to lowercase, the string becomes "raceacar", which is not a palindrome.
Example 3
Input:
s = " "
Output:
true
Note:
The string contains only non-alphanumeric characters. After filtering, it becomes an empty string, which is a valid palindrome.
Example 4
Input:
s = "Madam"
Output:
true
Note:
After filtering and converting to lowercase, the string becomes "madam", which is a palindrome.
Constraints
0 <= s.length <= 2 * 10^5s consists only of printable ASCII characters.2 * 10^5 characters, beyond a linear time complexity solution.Loading editor...