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) {
if (s.equals(""))
return true;
int pre = 0;
int post = s.length() - 1;
while (pre < post) {
if (!Character.isLetter(s.charAt(pre))&& !Character.isDigit(s.charAt(pre))) {
pre++;
}else if (!Character.isLetter(s.charAt(post))&& !Character.isDigit(s.charAt(post))) {
post--;
}else if (Character.toLowerCase(s.charAt(pre)) == Character.toLowerCase(s.charAt(post))) {
pre++;
post--;
}else
return false;
}
return true;
}
}