balance
to 0 to keep track of the balance of parentheses.moves
to 0 to count the number of moves required.s
.
a. If the character is an opening parenthesis '(', increment balance
.
b. If the character is a closing parenthesis ')', decrement balance
.
c. If balance
is negative, it means we have an unmatched closing parenthesis, so increment moves
and reset balance
to 0.balance
is positive, it means we have unmatched opening parentheses, so add balance
to moves
.moves
as the minimum number of moves required to make the string valid.