Share my Java AC solution


  • 0
    F
    public class Solution {
        public boolean isPalindrome(String s) {
            if(s == null) return false;
            if(s.length()==0) return true;
            char[] ca = s.toCharArray();
            int length = ca.length;
            int i=0, j=length-1;
            while(i<j){
                while (i<j && !isValidChar(ca[i])) i++;
                while (i<j && !isValidChar(ca[j])) j--;
                if (Character.toLowerCase(ca[i])!=Character.toLowerCase(ca[j])) return false;
                i++;
                j--;
            }
            return true;
        }
        
        public boolean isValidChar(char c) {
            return ('0'<=c && c<='9')||('A'<=c && c<='Z')||('a'<=c && c<='z');
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.