Minimum Remove to Make Valid Parentheses LeetCode solution in Python

1
class Solution:
2
def minRemoveToMakeValid(self, s):
3
stack = []
4
split_str = list(s)
5
for i in range(len(s)):
6
if s[i] == "(":
7
# if current char is '(' then push it to stack
8
stack.append(i)
9
elif s[i] == ")":
10
# if current char is ')' then pop top of the stack
11
if len(stack) != 0:
12
stack.pop()
13
else:
14
# if our stack is empty then we can't pop so make current char as ''
15
split_str[i] = ""
16
for i in stack:
17
split_str[i] = ""
18
return "".join(split_str)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0