A simple and elegant Java solution


  • 0
    L
    public boolean isPalindrome(String s) {
            if(s.length()==0) {
                return true;
            }
            int len = s.length();
            for(int i=0,j=len-1;j>i;) {
                while(i<len-1 && !Character.isLetterOrDigit(s.charAt(i))) {
                    i++;
                }
                
                while(j>=0 && !Character.isLetterOrDigit(s.charAt(j))) {
                    j--;
                }
                
                if(j>i && Character.toLowerCase(s.charAt(i)) !=Character.toLowerCase(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.