Share my simple java solution


  • 0
    J
    public boolean isPalindrome(String s) {
        if(s==null) return false;
        if(s.length()==1) return true;
        StringBuffer tmp=new StringBuffer();
        for(int i=0; i<s.length(); i++){
            if(s.charAt(i)>64 && s.charAt(i)<91) tmp.append((char)(s.charAt(i)+32));//convert 'A'-'Z' to 'a'-'z'
            if(s.charAt(i)>96 && s.charAt(i)<123) tmp.append(s.charAt(i));//keep 'a'-'z'
            if(s.charAt(i)>47 && s.charAt(i)<58) tmp.append(s.charAt(i));//keep  '0'-'9'
                                                                        //ignore other chars
        }
        String t=tmp.toString();
        
        int i=0;
        int j=t.length()-1;
        while(i<j){
            if(t.charAt(i)!=t.charAt(j)) return false;
            i++;
            j--;
        }
        
        return true;
    }

  • 0
    J
    public boolean isPalindrome(String s) {
        if(s==null) return false;
        if(s.length()==1) return true;
        s=s.toUpperCase();
        int i=0;
        int j=s.length()-1;
        while(i<j){
            if(!(s.charAt(i)>47 && s.charAt(i)<58 || s.charAt(i)>64 && s.charAt(i)<91 ) ){
                i++;
                continue;
            }
            if(!(s.charAt(j)>47 && s.charAt(j)<58 || s.charAt(j)>64 && s.charAt(j)<91 ) ){
                j--;
                continue;
            }
            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.