A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s
, return true
if it is a palindrome, or false
otherwise.
Example 1:
Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome.
Example 3:
Input: s = " " Output: true Explanation: s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:
1 <= s.length <= 2 * 105
s
consists only of printable ASCII characters.The problem asks to determine if a given string is a palindrome, considering only alphanumeric characters and ignoring case. The most efficient approach is using a two-pointer technique.
Algorithm:
Initialization: Two pointers, left
and right
, are initialized to point to the beginning and end of the string, respectively.
Iteration: The pointers move towards each other until they cross. In each iteration:
left
or right
is not alphanumeric (using isalnum()
or equivalent functions), the corresponding pointer is moved inwards.tolower()
or equivalent). If they differ, the string is not a palindrome, and false
is returned.Palindrome Confirmation: If the loop completes without finding unequal characters, the string is a palindrome, and true
is returned.
Time Complexity: O(n), where n is the length of the string. The pointers traverse the string at most once.
Space Complexity: O(1). The algorithm uses a constant amount of extra space, regardless of the input string size.
Code Examples (with explanations):
Python:
class Solution:
def isPalindrome(self, s: str) -> bool:
left, right = 0, len(s) - 1
while left < right:
while left < right and not s[left].isalnum():
left += 1 # Skip non-alphanumeric characters from the left
while left < right and not s[right].isalnum():
right -= 1 # Skip non-alphanumeric characters from the right
if s[left].lower() != s[right].lower():
return False # Characters don't match
left += 1
right -= 1
return True # All characters matched
Java:
class Solution {
public boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while (left < right) {
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
left++;
}
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
right--;
}
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
return false;
}
left++;
right--;
}
return true;
}
}
C++:
class Solution {
public:
bool isPalindrome(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
while (left < right && !isalnum(s[left])) {
left++;
}
while (left < right && !isalnum(s[right])) {
right--;
}
if (tolower(s[left]) != tolower(s[right])) {
return false;
}
left++;
right--;
}
return true;
}
};
The other languages (Go, TypeScript, Rust, JavaScript, C#, PHP) follow a similar structure, adapting the specific string manipulation and character checking functions of their respective languages. The core logic remains the same efficient two-pointer approach.