Skip to content

Bug Report for minimum-remove-to-make-valid-parentheses #4076

Closed
@nzwt

Description

@nzwt

Bug Report for https://neetcode.io/problems/minimum-remove-to-make-valid-parentheses

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
I got an error despite having a valid solution on leetcode

I got this error:
stderr

Traceback (most recent call last):
File "/box/script.py", line 141, in main
raise ValueError("Error: the string may contain only lowercase letters and parentheses.")
ValueError: Error: the string may contain only lowercase letters and parentheses.

Last executed test case

Input:

s="((((((dd(())a)())bbb)))()cccc((((("

Your Output:

"(((((dd(())a)())bbb)))()cccc"

Expected output:

"((((((dd(())a)())bbb))))cccc"

With this code:
def minRemoveToMakeValid(self, s: str) -> str:
#track positions of all parens, then find midpoint of each set
#this dosen't work becusuase you could have (bbep)(ppe)
#pair off each paren and you are left with any outliers
invalid = set()
res = ""
stack = []

    for i, c in enumerate(s):
        if c == "(":
            stack.append(i)
        elif c == ")":
            if not stack:
                invalid.add(i)
            else:
                stack.pop()

    invalid.update(stack)
    
    result = []
    for i, char in enumerate(s):
        if i not in invalid:
            result.append(char)
    
    return ''.join(result)
    
    return res

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