Skip to content

Commit 975b3ec

Browse files
Time: 1 ms (54.42%), Space: 38.5 MB (27.88%) - LeetHub
1 parent 4ad259f commit 975b3ec

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int minAddToMakeValid(String S) {
3+
int ans = 0, bal = 0;
4+
for (int i = 0; i < S.length(); ++i) {
5+
bal += S.charAt(i) == '(' ? 1 : -1;
6+
// It is guaranteed bal >= -1
7+
if (bal == -1) {
8+
ans++;
9+
bal++;
10+
}
11+
}
12+
13+
return ans + bal;
14+
}
15+
}

0 commit comments

Comments
 (0)