→ sustainer123: 大師 10/09 09:38
https://i.imgur.com/kyBhy6o.jpeg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.179 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1728437682.A.F7A.html
921. Minimum Add to Make Parentheses Valid
## 思路
字串只有左右括號
所以直接預設要加n個括號, 有valid的pair就-2
## Code
```python
class Solution:
def minAddToMakeValid(self, s: str) -> int:
res = len(s)
left = 0
for ch in s:
if ch == '(':
left += 1
elif left:
left -= 1
res -= 2
return res
```
--