921. Minimum Add to Make Parentheses Valid
/*
* @lc app=leetcode id=921 lang=cpp
*
* [921] Minimum Add to Make Parentheses Valid
*/
// @lc code=start
class Solution {
public:
int minAddToMakeValid(string S) {
const int N = S.length();
int res = 0, c = 0;
for (int i = 0; i < N; ++i) {
if (S[i] == '(') ++c;
else --c;
if (c < 0) {
++res;
++c;
}
}
return res + c;
}
};
// @lc code=end
Last updated