public class Solution {
public boolean isPalindrome(String s) {
if (s == null) {
return false;
}
int start = 0;
int end = s.length() - 1;
while (start < end) {
while (start < end && !isValid(s.charAt(start))) {
start++;
}
while (start < end && !isValid(s.charAt(end))) {
end--;
}
if (start < end && !equalsIgnoreCase(s.charAt(start), s.charAt(end))) {
return false;
}
start++;
end--;
}
return true;
}
private boolean equalsIgnoreCase(char a, char b) {
if (a == b) {
return true;
}
if (isChar(a) && isChar(b) && Math.abs(a - b) == Math.abs('A' - 'a')) {
return true;
}
return false;
}
private boolean isValid(char a) {
return isChar(a) || (a >= '0' && a <= '9');
}
private boolean isChar(char a) {
if (a >= 'a' && a <= 'z') {
return true;
}
if (a >= 'A' && a <= 'Z') {
return true;
}
return false;
}
}
Character.toLowerCase() would make it easier :P
没有评论:
发表评论