AC Java solution 4ms


  • 0
    L

    98%, not sure how it is possible..

    public class Solution {
        public char getChar(String s, int index) {
            char c = s.charAt(index);
            
            if (c >= '0' && c <= '9')
                return c;
                
            if (c >= 'a' && c <= 'z')
                return c;
                
            if (c >= 'A' && c <= 'Z')
                return (char)(c + 'a' - 'A');
                
            return 0;
        }
        
        public boolean isPalindrome(String s) {
            int len = s.length();
            if (len <= 1)
                return true;
            
            int l = 0;
            int r = len - 1;
            char cl, cr;
            
            while (l < len && r >= 0) {
                cl = getChar(s, l);
                while (cl == 0 && l < len - 1) {
                    l++;
                    cl = getChar(s, l);
                }
                if (cl == 0)
                    break;
                
                cr = getChar(s, r);
                while (cr == 0 && r > 0) {
                    r--;
                    cr = getChar(s, r);
                }
                if (cr == 0)
                    break;
    
                if (cl != cr)
                    return false;
                    
                if (l >= r - 1)
                    break;
    
                l++;
                r--;
            }
            
            return true;
        }
    }
    

Log in to reply
 

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