Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nzwt opened this issue Apr 24, 2025 · 2 comments
Open

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

nzwt opened this issue Apr 24, 2025 · 2 comments

Comments

@nzwt
Copy link

nzwt commented Apr 24, 2025

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
@Srihari2222
Copy link
Collaborator

@nzwt Thanks for the report. Sorry for the bug you faced. There will be some changes made to the driver code on the backend in short period and will resolve this issue.

@MasterKinjalk
Copy link

Just came here to report the same bug. I hope it gets rectified soon.

I will add some more details, just in case.

Bug Report for "Minimum Remove to Make Valid Parentheses"

Description

I believe there's an issue with test case validation for the "Minimum Remove to Make Valid Parentheses" problem. The problem statement indicates that multiple valid solutions should be accepted, but the test case validator seems to be expecting a specific valid solution rather than accepting any valid solution.
Problem Statement Excerpt
The problem states: "Return the resulting string after removing the invalid parentheses." And in Example 1, it explicitly mentions: "Explanation: 'nee(t(co)de)', 'nee(t(c)ode)' would also be accepted."
Specific Test Case Issue
For the input: "((((((dd(())a)())bbb)))()cccc((((("
My output: "(((((dd(())a)())bbb)))()cccc"
Expected: "((((((dd(())a)())bbb))))cccc"
Both solutions are valid according to the problem's definition of validity:

Both remove the minimum number of parentheses (5 unmatched opening parentheses at the end)
Both resulting strings have balanced parentheses

Implementation

My solution correctly identifies unmatched parentheses using a stack-based approach. For this specific test case, it removes the 5 unmatched opening parentheses at the end, but differs from the expected output in which specific parenthesis pair is kept near the "cccc" section.

Requested Action

Please verify that the test case validator is properly accepting any valid solution as mentioned in the problem statement, rather than requiring a specific valid solution.

The code to reproduce the error:

class Solution:
    def minRemoveToMakeValid(self, s: str) -> str:
        stack_dict = dict()
        ignore_index_edge = []
        for i in range(len(s)):
            #Edge case must be a open bracket cannot be a close for the first bracket.
            if len(stack_dict) == 0 and s[i] == ")":
                ignore_index_edge.append(i)
                continue
            if s[i] == "(" or s[i] == ")":
                stack_dict[i] = s[i]
        
        stack = []
        index_val = []
        sorted_dict = dict(sorted(stack_dict.items()))
        #Now use stack to determine the correctness. 
        for key,value in sorted_dict.items():
            if value == "(":
                stack.append(value)
                index_val.append(key)
            elif value == ")" and len(stack)>0:
                stack.pop()
                index_val.pop()
            elif  value == ")":
                stack.append(value)
                index_val.append(key)



        final_removable_index = sorted(set([*index_val, *ignore_index_edge]), reverse = True)
        print(index_val)
        temp_s = list(s)
        for i in final_removable_index:
            del temp_s[i]
        print(final_removable_index)
        print(stack_dict)
        return "".join(temp_s)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants