Java solution beats 94%


  • 0
    O
    public boolean isPalindrome(String s) {
            int lo = 0;
            int hi = s.length() - 1;
            char l, h;
            while(lo < hi) {
                l = Character.toLowerCase(s.charAt(lo));
                h = Character.toLowerCase(s.charAt(hi));
                while(lo < hi && !isAlphanumeric(l)) {
                    lo++;
                    l = Character.toLowerCase(s.charAt(lo));
                }
                while(lo < hi && !isAlphanumeric(h)) {
                    hi--;
                    h = Character.toLowerCase(s.charAt(hi));
                }
                if(h != l) return false;
                lo++;
                hi--;
            }
            return true; // better ask if empty string is considered palindrome or not
        }
        
        private boolean isAlphanumeric(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.