921. Minimum Add to Make Parentheses Valid
Question 921
Input: "())"
Output: 1Answer
class Solution(object):
def minAddToMakeValid(self, S):
Sstack = []
Wrong = []
for s in S:
if s == "(":
Sstack.append(s)
elif s == ")":
if len(Sstack) < 1:
Wrong.append(s)
else:
Sstack.pop()
return len(Sstack) + len(Wrong)class Solution(object):
def minAddToMakeValid(self, S):
Sstack = 0
Wrong = 0
for s in S:
if s == "(":
Sstack += 1
elif s == ")":
if Sstack < 1:
Wrong += 1
else:
Sstack -= 1
return Sstack + Wrong
Last updated