We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f49e32e commit 9276985Copy full SHA for 9276985
Strings/921_Minimum_Add_to_Make_Parentheses_Valid.java
@@ -0,0 +1,22 @@
1
+class Solution {
2
+ public int minAddToMakeValid(String S) {
3
+ if (S == null || S.length() == 0) {
4
+ return 0;
5
+ }
6
+
7
+ int result = 0;
8
+ Stack<Character> s = new Stack<>();
9
10
+ for (char c : S.toCharArray()) {
11
+ if (c == '(') {
12
+ s.push(c);
13
+ } else if (s.isEmpty()) {
14
+ ++result;
15
+ } else {
16
+ s.pop();
17
18
19
20
+ return result + s.size();
21
22
+}
0 commit comments