public class Solution {
/**
* @param s: A string
* @return: Whether the string is a valid palindrome
*/
public boolean isPalindrome(String s) {
// write your code here
s = s.toLowerCase();
int i = 0, j = s.length() - 1;
while(i < j) {
if(!Character.isLetterOrDigit(s.charAt(i))) {
i++;
continue;
}
if(!Character.isLetterOrDigit(s.charAt(j))) {
j--;
continue;
}
char leftChar = s.charAt(i++);
char rightChar = s.charAt(j--);
if(leftChar != rightChar) {
return false;
}
}
return true;
}
}
网友评论