Minimum Add to Make Parentheses Valid
Last updated
Last updated
Input:
"())"
Output:
1Input:
"((("
Output:
3Input:
"()"
Output:
0Input:
"()))(("
Output:
4class Solution:
def minAddToMakeValid(self, S):
"""
:type S: str
:rtype: int
"""
left = right = 0
for i in S:
if i == '(':
left +=1
elif left<=0:#都平衡了,再多个)就不行了 所以要<=
right+=1
else:
left -= 1
return left + right