Skip to content

Commit 5ef2cd2

Browse files
authored
Create minimum-add-to-make-parentheses-valid.cpp
1 parent 8d28e56 commit 5ef2cd2

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int minAddToMakeValid(string S) {
7+
int add = 0, bal = 0;
8+
for (const auto& c : S) {
9+
bal += c == '(' ? 1 : -1;
10+
if (bal == -1) {
11+
++add;
12+
++bal;
13+
}
14+
}
15+
return add + bal;
16+
}
17+
};

0 commit comments

Comments
 (0)