Question:

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false

二刷

class Solution {
    public boolean isPalindrome(String s) {
        if(s == null) return false;
        s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        int len = s.length();
        if(len == 0) return true;
        int head = 0, end = s.length() - 1;
        char[] arr = s.toCharArray();
        while(head < end){
            while(!isAlphanumeric(arr[head])){
                if(head + 1 >= len) return true;
                head++;
            }
            while(!isAlphanumeric(arr[end])){
                if(end - 1 < 0) return true;
                end--;
            }
            if(head > end) return true;
            if(arr[head++] != arr[end--]) return false;
        }
        return true;
    }
    public boolean isAlphanumeric(char c) {
		if ((c >=48 && c <=57) || (c >= 97 && c <= 122)) return true;
		return false;
	}
}