Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case.
Description: In this problem, we define the empty string as a valid palindromic sequence.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Thinking: This is a study of the palindrome title, and is the simplest form, that is, whether a string is a palindrome.
To address this problem, we can use the dual head and tail pointer,
- If two pointers are different elements, the process directly returns false,
- If the two pointers same elements, we also update the head and tail pointers the loops. Until the head and tail pointers meet.
class Solution {
public boolean isPalindrome(String s) {
int i=0;int j=s.length()-1;
while(i<j)
{
if(!Character.isLetterOrDigit(s.charAt(i))){
i++;
continue;
}
if(!Character.isLetterOrDigit(s.charAt(j))){
j--;
continue;
}
if(Character.toLowerCase(s.charAt(i))!=Character.toLowerCase(s.charAt(j)))
return false;
i++;j--;
}
return true;
}
}