Simple, fast java solution( 7ms ) beats 87% submissions


  • 0
    M
    private boolean isAlphaNum(char c){
            return 
                (c>='a' && c<='z') || 
                (c>='A' && c<='Z') || 
                (c>='0' && c<='9');    
        }
        
        private boolean same(char c1, char c2){
            return Character.toLowerCase(c1) == Character.toLowerCase(c2);
        }
        
        public boolean isPalindrome(String s) {
            
            if(s==null || s.length()==0){ return true; }
            
            char a[] = s.toCharArray();
            
            for(int i=0, j=a.length-1; i<j; ){
                
                if(!isAlphaNum(a[i])){ i++; }
                
                else if(!isAlphaNum(a[j])){ j--; }
                
                else if(!same(a[i++], a[j--])){
                    return false;
                }
            }
            return true;
        }
    

Log in to reply
 

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