11 ms Java solution beats 39%


  • 0

    public class Solution {
    public boolean isPalindrome(String s) {
    s=s.toLowerCase();

        s=s.trim();
        
        if(s.length()==1||s.length()==0)
        return true;
     
         
        int c=0,d=0;
        for(int i=0,j=s.length()-1;i<=j;){
            
            c=0;d=0;
            if(s.charAt(i)==s.charAt(j))
               {
                  
                   i++;
                   j--;
                   continue;
                   
               }
               
               if(s.charAt(i)==' '||!((s.charAt(i)>=97&&s.charAt(i)<=122)||(s.charAt(i)>=48&&s.charAt(i)<=57)))
                 { c=1;
                     i++;
                 }
            
               if(s.charAt(j)==' '||!((s.charAt(j)>=97&&s.charAt(j)<=122)||(s.charAt(j)>=48&&s.charAt(j)<=57))){
                  d=1;
                 j--;
             
                 }
                 
                  if(s.charAt(i)!=s.charAt(j)&&(c!=1&&d!=1))
                  {
              return false;
                  }
                  
          
              
       }
        return true;
    }
    

    }


Log in to reply
 

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