Related: Minimum number of bracket reversals needed to make an expression balanced
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/discuss/181132/C%2B%2BJavaPython-Straight-Forward-One-Pass
X. https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/discuss/181092/Java-Solution-From-Two-Pass-to-One-Pass1
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
Given a string
S
of '('
and ')'
parentheses, we add the minimum number of parentheses ( '('
or ')'
, and in any positions ) so that the resulting parentheses string is valid.
Formally, a parentheses string is valid if and only if:
- It is the empty string, or
- It can be written as
AB
(A
concatenated withB
), whereA
andB
are valid strings, or - It can be written as
(A)
, whereA
is a valid string.
Given a parentheses string, return the minimum number of parentheses we must add to make the resulting string valid.
Keep track of the balance of the string: the number of
'('
's minus the number of ')'
's. A string is valid if its balance is 0, plus every prefix has non-negative balance.
The above idea is common with matching brackets problems, but could be difficult to find if you haven't seen it before.
Now, consider the balance of every prefix of
S
. If it is ever negative (say, -1), we must add a '(' bracket. Also, if the balance of S
is positive (say, +B
), we must add B
')' brackets at the end.
public int minAddToMakeValid(String S) {
int ans = 0, bal = 0;
for (int i = 0; i < S.length(); ++i) {
bal += S.charAt(i) == '(' ? 1 : -1;
// It is guaranteed bal >= -1
if (bal == -1) {
ans++;
bal++;
}
}
return ans + bal;
}
To make a string valid,
we can add some
and add some
we can add some
(
on the left,and add some
)
on the right.
We need to find the number of each.
Explanation:
left
records the number of (
we need to add.right
records the current opened parentheses.
Loop char
If
it means no left parentheses can mathch this right parentheses,
we have to add one more left parentheses on the left of the string.
Otherwise, we update
i
on the string:If
right == 0 && i == ')'
,it means no left parentheses can mathch this right parentheses,
we have to add one more left parentheses on the left of the string.
Otherwise, we update
right
.
Return
left + right
in the end public int minAddToMakeValid(String S) {
int left = 0, right = 0;
for (char i : S.toCharArray()) {
if (right == 0 && i == ')')
left++;
else
right += i == '(' ? 1 : -1;
}
return left + right;
}
X. https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/discuss/181086/Java-two-one-pass-7-liners-space-O(n)-and-O(1)-respectively
Loop through the input array.
- if encounter '(', push '(' into stack;
- otherwise, ')', check if there is a matching '(' on top of stack; if no, increase the counter by 1; if yes, pop it out;
- after the loop, count in the un-matched characters remaining in the stack.
public int minAddToMakeValid(String S) {
if (S == null || S.length() == 0) {
return 0;
}
int open = 0;
int res = 0;
for (char c : S.toCharArray()) {
if (c == '(') {
open++;
} else {
open--;
if (open < 0) {
res++;
open++;
}
}
}
int close = 0;
for (int i = S.length() - 1; i >= 0; i--) {
char c = S.charAt(i);
if (c == ')'){
close++;
} else {
close--;
if (close < 0) {
res++;
close++;
}
}
}
return res;
}
http://www.noteanddata.com/leetcode-921-Minimum-Add-to-Make-Parentheses-Valid-solution-note.html
首先想到的是能不能直接遍历一下字符串,然后返回Math.abs(leftcount-rightcount),
但是看到"))(("这样的case显然不能通过,所以在遍历的过程中,如果右括号不能被匹配,那么就直接需要计数了。
也就是常规的用stack的思路,如果遇到左括号就push,遇到右括号就pop。 但是如果遇到右括号的时候stack是空的,那就要计数了。
还有最后剩下的左括号个数也需要加上。
由于这里只需要返回个数,所以不需要用stack,直接用两个count就好。
但是看到"))(("这样的case显然不能通过,所以在遍历的过程中,如果右括号不能被匹配,那么就直接需要计数了。
也就是常规的用stack的思路,如果遇到左括号就push,遇到右括号就pop。 但是如果遇到右括号的时候stack是空的,那就要计数了。
还有最后剩下的左括号个数也需要加上。
由于这里只需要返回个数,所以不需要用stack,直接用两个count就好。
public int minAddToMakeValid(String S) {
int leftcount = 0, missingleft = 0;
for(int i = 0; i < S.length(); ++i) {
if('(' == S.charAt(i)) {
leftcount++;
}
else {
leftcount--;
if(leftcount < 0) {
missingleft++;
leftcount = 0;
}
}
}
return leftcount + missingleft;
}