12ms Java Solution


  • 0
    A
    public boolean isPalindrome(String s) {
            
            if(s == null || s.isEmpty())
                return true;
    
            s = s.trim().toLowerCase();
    
            int i = 0;
            int j = s.length()-1;
    
            while(i < j){
    
                while(!Character.isLetterOrDigit(s.charAt(i)) && i < j)
                    i++;
                while(!Character.isLetterOrDigit(s.charAt(j)) && j > i)
                    j--;
    
                if(s.charAt(i) != s.charAt(j))
                    return false;
    
                i++;
                j--;
    
            }
    
            return true;
                
                
        }
    

Log in to reply
 

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