neetcode-gh / leetcode

Leetcode solutions
MIT License
5.48k stars 2.27k forks source link

Valid Palindrome #2940

Open msbazzik opened 1 year ago

msbazzik commented 1 year ago

Not fully agree, that String of " "(whitespace) is palindrome, as it doesn't contain any characters after trimming. In other words, the Strings "" and " "(which is actually "" after removing all non-alphanumeric characters ) should return false.

aviralrabbit1 commented 1 year ago

Which language code has this issue?

msbazzik commented 1 year ago

Hi there, thank you for getting back. Here is the code:

public boolean isPalindrome(String s) {

int i = 0;
int j = s.length() - 1;
while (i < j) {

    Character start = s.charAt(i);
    Character end = s.charAt(j);

    if (!Character.isLetterOrDigit(start)) {
        i++;
        continue;
    }

    if (!Character.isLetterOrDigit(end)) {
        j--;
        continue;
    }

    if (Character.toLowerCase(start) != Character.toLowerCase(end)) {
        return false;
    }

    i++;
    j--;    
}

return true;

} Both strings of "" (empty) and " " (whitespace) are palindromes according to solution from the neetcode.

brayo-pip commented 12 months ago

which language though? Java?

msbazzik commented 12 months ago

Hi, oh yes, I forgot to mention that it's Java, sorry