Skip to content

Bug Report for is-palindrome #5664

@devansh-m100

Description

@devansh-m100

Bug Report for https://neetcode.io/problems/is-palindrome

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

The 2nd solution Two Pointer based given in solution tab by NeetCode itself, is a buggy code and it fails when clicked submit button. The actual update is to be done in case of - the r > 1 is to be replaced by l < r in the 2nd inner while loop even.

Image

Given Python model solution in solution tab -

class Solution:
def isPalindrome(self, s: str) -> bool:
l, r = 0, len(s) - 1

    while l < r:
        while l < r and not self.alphaNum(s[l]):
            l += 1
        while r > l and not self.alphaNum(s[r]):
            r -= 1
        if s[l].lower() != s[r].lower():
            return False
        l, r = l + 1, r - 1
    return True

def alphaNum(self, c):
    return (ord('A') <= ord(c) <= ord('Z') or
            ord('a') <= ord(c) <= ord('z') or
            ord('0') <= ord(c) <= ord('9'))

Correct model solution to update in solution tab -

class Solution:
def isPalindrome(self, s: str) -> bool:
l, r = 0, len(s) - 1

    while l < r:
        while l < r and not self.isAlphaNum(s[l]):
            l += 1
        while l < r and not self.isAlphaNum(s[r]):
            r -= 1
        if s[l].lower() != s[r].lower():
            return False
        l, r = l + 1, r - 1
    return True

def isAlphaNum(self, c):
    return (
        ord('A') <= ord(c) <= ord('Z') or
        ord('a') <= ord(c) <= ord('z') or
        ord('0') <= ord(c) <= ord('9')
    )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions