Skip to content

Commit 068de55

Browse files
committed
Added Java solution for "Minimum Add to Make Parentheses Valid"
1 parent 33a2e82 commit 068de55

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
3+
/**
4+
* Time: O(n)
5+
* Memory: O(1)
6+
*/
7+
public int minAddToMakeValid(String s) {
8+
int add = 0, score = 0;
9+
10+
for (int i = 0; i < s.length(); ++i) {
11+
score += s.charAt(i) == '(' ? 1 : -1;
12+
if (score == -1) {
13+
++add;
14+
++score;
15+
}
16+
}
17+
18+
return add + score;
19+
}
20+
}

0 commit comments

Comments
 (0)