Simple 6ms Java solution


  • -1
    G

    \I found that it would be slower if I use regular expression, and replaceAll() function.

    public class Solution {

    public boolean isPalindrome(String s) {
        int i = 0, j =s.length() -1;
        while(i < j){
            while(!isAlphaNum(s.charAt(i))){
                if(i < j){ i++;}
                else{ return true;}
            }
            while(!isAlphaNum(s.charAt(j))){ 
                if(i < j){ j--;}
                else{ return true;}
            }
            
            int dif = s.charAt(i) - s.charAt(j);
            dif = dif>0?dif:-dif;
            if(dif != 0 && dif != 32){
                return false;
            }
            i++;
            j--;
        }
        return true;
    
    }
    public boolean isAlphaNum(char m){
        if('0' <= m && m <= '9'){ return true;}
        else if('a' <= m && m <= 'z'){ return true;}
        else if('A' <= m && m <= 'Z'){ return true;}
        else{ return false;}
    }
    

    }


  • 1
    V

    Your code doesn't pass "0P","1Q","2R"...."9Y".


  • 0
    G

    Oh, right! thanks!


Log in to reply
 

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