Example: Valid Palindrome - rFronteddu/general_wiki GitHub Wiki
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.
Solution
To determine if a given string is a palindrome after converting all uppercase letters to lowercase and removing all non-alphanumeric characters, you can follow these steps:
- Normalize the String: Convert all characters to lowercase and remove non-alphanumeric characters.
- Check for Palindrome: Use two pointers to compare characters from the start and end of the string, moving towards the center.
public class Solution {
public boolean isPalindrome(String s) {
// Convert the string to lowercase and remove non-alphanumeric characters
s = s.toLowerCase().replaceAll("[^a-z0-9]", "");
// Initialize two pointers
int left = 0, right = s.length() - 1;
// Compare characters from both ends towards the center
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false; // Characters do not match
}
left++;
right--;
}
return true; // All characters matched
}
}