My 5ms Java Solution using two pointers, beat 95% submission


  • 0
    B
    public boolean isPalindrome(String s) {
            if(s.trim().equals("")) {
                return true;
            }
            char[] arr = s.toCharArray();
            int left = 0;
            int right = arr.length - 1;
            while (left < right) {
                if (arr[left] < '0' || arr[left] > '9' && arr[left] < 'A' || arr[left] > 'Z' && arr[left] < 'a' || arr[left] > 'z') {
                    left ++;
                    continue;
                }
                if (arr[right] < '0' || arr[right] > '9' && arr[right] < 'A' || arr[right] > 'Z' && arr[right] < 'a' || arr[right] > 'z') {
                    right --;
                    continue;
                }
                if (Character.toUpperCase(arr[left]) != Character.toUpperCase(arr[right])) {
                    return false;
                }
                left++;
                right--;
            }
            return true;
    

Log in to reply
 

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