Description
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