-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution921.java
More file actions
38 lines (32 loc) · 820 Bytes
/
Solution921.java
File metadata and controls
38 lines (32 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package algorithm.leetcode;
/**
* @author: mayuan
* @desc:
* @date: 2019/02/09
*/
public class Solution921 {
public static void main(String[] args) {
String str1 = "()))((";
Solution921 solution921 = new Solution921();
System.out.println(solution921.minAddToMakeValid(str1));
}
public int minAddToMakeValid(String S) {
if (null == S || 1 > S.length()) {
return 0;
}
int left = 0, right = 0;
for (int i = 0; i < S.length(); ++i) {
char c = S.charAt(i);
if ('(' == c) {
++left;
} else {
if (0 < left) {
--left;
} else {
++right;
}
}
}
return left + right;
}
}