7ms java solution


  • 0
    L
    public boolean isPalindrome(String s) {
        if(s == null) return true;
        int start = 0;
        int end = s.length()-1;
        char l,r;
        while(start <= end){
            l = s.charAt(start);
            r = s.charAt(end);
            while(start < end && !isAlphanumeric(l)) {
               start++;
               l = s.charAt(start); 
              
            }
            while(start < end && !isAlphanumeric(r)){
                end--;
                r = s.charAt(end);
            } 
            if(start <= end){
                if(isDigit(l) && !isDigit(r) || !isDigit(l) && isDigit(r))
                    return false;
                if(l != r && Math.abs(l-r) != 32)
                    return false;
            }
            start++;
            end--;
        }
        return true;
    }
    private boolean isAlphanumeric(char c){
        if(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9') return true;
        return false;
    } 
    private boolean isDigit(char c){
        if(c >= '0' && c <= '9') return true;
        return false;
    }

Log in to reply
 

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