-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
41 lines (34 loc) · 1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution:
def minRemoveToMakeValid(self, s: str) -> str:
should_remove = set()
stack = []
for i, c in enumerate(s):
if c == "(":
stack.append(i)
elif c == ")":
if len(stack) == 0:
should_remove.add(i)
else:
stack.pop()
for i in stack:
should_remove.add(i)
result = []
for i, c in enumerate(s):
if i not in should_remove:
result.append(c)
return "".join(result)
class Solution:
def minRemoveToMakeValid(self, s: str) -> str:
result = [c for c in s]
stack = []
for i, c in enumerate(s):
if c == "(":
stack.append(i)
elif c == ")":
if len(stack) == 0:
result[i] = ""
else:
stack.pop()
for i in stack:
result[i] = ""
return "".join(result)