7ms O(n) Java solution without using built-in Character.isLetterOrDigit


  • 0
    A
    public boolean isPalindrome(String s) {
        char[] chars = s.toLowerCase().toCharArray();
        int left = 0, right = s.length()-1;
        while(left<right){
            while(left<right && !((chars[left]>='a' && chars[left]<='z') || (chars[left]>='0' && chars[left]<='9'))) left++;
            while(left<right && !((chars[right]>='a' && chars[right]<='z') || (chars[right]>='0' && chars[right]<='9'))) right--;
            if(chars[left++]!=chars[right--]) return false;
        }
        return true;
    }

Log in to reply
 

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