· 1 min read

Blind 75 - Valid Palindrome

Have 2 pointers. One at the start other at the end. Skip if `!Character.isLetterOrDigit` and compare with `Character.toLowerCase`

Link

Learning From

NeetCode

Asked at

No where

Source

Video

https://youtu.be/7KHKFqLPMbs

Problem and Constraints

All Approaches and Explanations in English

O(n) solution. O(1) space complexity

Have 2 pointers. One at the start other at the end. Skip if !Character.isLetterOrDigit and compare with Character.toLowerCase

Code, if any

class Solution {
    public boolean isPalindrome(String s) {
        int l = 0;
        int r = s.length() -1;
        while(l<r){
            char left = s.charAt(l);
            if(!Character.isLetterOrDigit(left)){
                l++;
                continue;
            }
            char right= s.charAt(r);
            if(!Character.isLetterOrDigit(right)){
                r--;
                continue;
            }
            if(Character.toLowerCase(left) != Character.toLowerCase(right)){
                return false;
            }
            l++;
            r--;
        }
        return true;
    }
}

Back to Blog