My java code with O(n) using 7ms


  • 0
    W
    public boolean isPalindrome(String s) {
        int len = s.length();
        int left = 0;
        int right = len-1;
        char leftChar;
        char rightChar;
        while(left<right){
            while(left<len && isChar(s.charAt(left)) == ' '){
                left++;
            }
            
            while(right>=0 && isChar(s.charAt(right)) == ' '){
                right--;
            }
           
            if(right>=0 && left<len){
                leftChar = isChar(s.charAt(left));
                rightChar = isChar(s.charAt(right));
                if(leftChar != rightChar)
                    return false;
            }
            left++;
            right--;
        }
        return true;
    }
    public char isChar(char c){
        if(c>=65 && c<=90){
            return (char)(c+32);
        }
        else if(c>=97 && c<=122){
            return c;
        }
        else if(c>=48 && c<= 57){
            return c;
        }
        return ' ';
    }

Log in to reply
 

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