Valid Palindrome

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.length() == 0) {
        	return true;
        }
        s = s.toUpperCase();
        int first = 0;
        int end = s.length()-1;
        while (first < end) {
        	if ((s.charAt(first)<'A' || s.charAt(first)>'Z') && (s.charAt(first)>'9' || s.charAt(first)<'0' )) {
        		first++;
        		continue;
        	}
        	if ((s.charAt(end)<'A' || s.charAt(end)>'Z') && (s.charAt(end)>'9' || s.charAt(end)<'0' )) {
        		end--;
        		continue;
        	}
        	if (s.charAt(first) == s.charAt(end)) {
        		first++;
        		end--;
        	} else {
        		return false;
        	}
        }
        return true;
    }
}

 

你可能感兴趣的:(Valid Palindrome)