Problem Description
Determine whether a given string is a palindrome after converting all uppercase letters into lowercase and removing all non-alphanumeric characters. An empty string is considered a palindrome.
Key Insights
- Use two pointers (one starting at the beginning and one at the end) to compare characters.
- Skip characters that are not alphanumeric.
- Compare characters in a case-insensitive manner.
- Continue until the pointers meet or cross.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the string, as each character is processed at most once. Space Complexity: O(1), using only a few extra variables for pointers and temporary comparisons.
Solution
The solution employs a two-pointer approach:
- Initialize two pointers: one at the start (left) and one at the end (right) of the string.
- Skip any non-alphanumeric characters using these pointers.
- Convert the remaining characters at each pointer to lowercase and compare them.
- If any mismatch is found, return false; otherwise, continue until the pointers cross.
- Return true if all corresponding characters match. This method efficiently checks for a palindrome without needing additional space for a filtered string.