:type S: str :rtype: int
(self, S)
| 44 | """ |
| 45 | class Solution(object): |
| 46 | def minAddToMakeValid(self, S): |
| 47 | """ |
| 48 | :type S: str |
| 49 | :rtype: int |
| 50 | """ |
| 51 | if not S: |
| 52 | return 0 |
| 53 | t = [S[0]] |
| 54 | for i in S[1:]: |
| 55 | if i == ')': |
| 56 | if not t: |
| 57 | t.append(i) |
| 58 | continue |
| 59 | |
| 60 | if t[-1] == '(': |
| 61 | t.pop() |
| 62 | else: |
| 63 | t.append(i) |
| 64 | else: |
| 65 | t.append(i) |
| 66 | return len(t) |
| 67 |