Clean Java solution ~10ms


  • 0
    S
    public boolean isPalindrome(String s) {
        
        s = s.toLowerCase();
        int i = 0;
        int j = s.length() - 1;
        
        while (i < s.length()-1 && j >= i){
            if (!isValid(s.charAt(i))){
                i++;
                continue;
            }
            if (!isValid(s.charAt(j))){
                j--;
                continue;
            }
            if (s.charAt(i) != s.charAt(j)){
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
    
    private boolean isValid(char c){
        return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
    }

Log in to reply
 

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