Skip to content

Commit 9f497ce

Browse files
authored
Create minimum-remove-to-make-valid-parentheses.py
1 parent b8e64b0 commit 9f497ce

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def minRemoveToMakeValid(self, s):
6+
"""
7+
:type s: str
8+
:rtype: str
9+
"""
10+
result = list(s)
11+
count = 0
12+
for i, v in enumerate(result):
13+
if v == '(':
14+
count += 1
15+
elif v == ')':
16+
if count:
17+
count -= 1
18+
else:
19+
result[i] = ""
20+
if count:
21+
for i in reversed(xrange(len(result))):
22+
if result[i] == '(':
23+
result[i] = ""
24+
count -= 1
25+
if not count:
26+
break
27+
return "".join(result)

0 commit comments

Comments
 (0)