Skip to content

Commit 8d28e56

Browse files
authored
Create minimum-add-to-make-parentheses-valid.py
1 parent 43fd91e commit 8d28e56

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def minAddToMakeValid(self, S):
6+
"""
7+
:type S: str
8+
:rtype: int
9+
"""
10+
add, bal, = 0, 0
11+
for c in S:
12+
bal += 1 if c == '(' else -1
13+
if bal == -1:
14+
add += 1
15+
bal += 1
16+
return add + bal

0 commit comments

Comments
 (0)