Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama"
is a palindrome.
"race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
public class Solution {
public boolean isPalindrome(String s) {
int tmp ='A'-'a';
int i=0;
int j=s.length()-1;
while(i<=j){
while(i<s.length()){
if('a'<=s.charAt(i)&&s.charAt(i)<='z') break;
else if('A'<=s.charAt(i)&&s.charAt(i)<='Z') break;
else if('0'<=s.charAt(i)&&s.charAt(i)<='9') break;
else i++;
}
while(0<=j){
if('a'<=s.charAt(j)&&s.charAt(j)<='z') break;
else if('A'<=s.charAt(j)&&s.charAt(j)<='Z') break;
else if('0'<=s.charAt(j)&&s.charAt(j)<='9') break;
else j--;
}
if(i==s.length()) return true;
if(s.charAt(i)==s.charAt(j)){
i++;
j--;
}
else if(tmp+s.charAt(i)==s.charAt(j)){
i++;
j--;
}
else if(s.charAt(i)-tmp==s.charAt(j)){
i++;
j--;
}
else return false;
}
return true;
}
}