Simple solution


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

Log in to reply
 

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