# 921. Minimum Add to Make Parentheses Valid

{% embed url="<https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/>" %}

跟 Q1249 其实是同样的题，因为只要计数而不要获得结果，还容易些。

```python
class Solution:
    def minAddToMakeValid(self, S: str) -> int:
        stack = []
        res = 0
        for i, c in enumerate(S):
            if c == '(':
                stack.append(c)
            elif c == ')':
                if stack:
                    stack.pop()
                else:
                    res += 1
        res += len(stack)
        return res
```
