125. Valid Palindrome

来源:互联网 发布:知轩藏书 编辑:程序博客网 时间:2024/06/05 10:15

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;        int low=0;        int high=s.length()-1;        while(low<=high){            while(low<=high&&(!valid(s.charAt(low))))                low++;            while(high>=low&&(!valid(s.charAt(high))))                high--;            if(low>=high)                return true;            if(change(s.charAt(low))!=change(s.charAt(high)))                return false;            low++;            high--;            }        return true;                        }    static boolean valid(char c){        if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))            return true;        else            return false;    }    static char change(char c){        if(c>='a'&&c<='z'){            char temp=(char)(c-('a'-'A'));            return temp;        }        return c;        }}


0 0
原创粉丝点击