Skip to content

ยท 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;
    }
}

Get new posts by email

No spam. Unsubscribe anytime. Or grab the RSS feed.

Mailto-based for now. ButtonDown / ConvertKit can replace this form later.


Comments lazy-load on scroll. Prefer GitHub? Giscus activates once PUBLIC_GISCUS_* env vars are set (see Giscus.astro).

Index

Index

Back to Blog