Bug Report for minimum-remove-to-make-valid-parentheses
Bug Report for https://neetcode.io/problems/minimum-remove-to-make-valid-parentheses
Tried to submit a solution for this problem but an alternative output which should be an acceptable answer isn't accepted
class Solution:
def minRemoveToMakeValid(self, s: str) -> str:
l = list(s)
st = []
for i in range(len(l)):
if l[i] == '(':
st.append(i)
elif l[i] == ')':
if st:
st.pop()
else:
l[i] = ''
while st:
i = st.pop()
l[i] = ''
return ''.join(l)
Output - (((((dd(())a)())bbb)))()cccc
But only acceptable output is
((((((dd(())a)())bbb))))cccc
PS same solution works on Leetcode with the same input and code
Wanted to add onto this: The printed Java solution under 3. Stack (Optimized) gives the same output as the user above and also will not submit. Should be an acceptable output.
3. Stack (Optimal)
public class Solution {
public String minRemoveToMakeValid(String s) {
StringBuilder sb = new StringBuilder(s);
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '(') {
stack.push(i);
} else if (sb.charAt(i) == ')') {
if (!stack.isEmpty()) {
stack.pop();
} else {
sb.setCharAt(i, '\0');
}
}
}
while (!stack.isEmpty()) {
sb.setCharAt(stack.pop(), '\0');
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) != '\0') {
result.append(sb.charAt(i));
}
}
return result.toString();
}
}
Input:
s="((((((dd(())a)())bbb)))()cccc((((("
Your Output
"(((((dd(())a)())bbb)))()cccc"
Expected output:
"((((((dd(())a)())bbb))))cccc"
Thanks for the feedback! The issue has been resolved now.