-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.java
32 lines (24 loc) · 915 Bytes
/
Solution.java
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
import java.util.Stack;
public class Solution {
public static long solve(String s) {
Stack<Integer> openingParenthesis = new Stack<>();
long missingOpeningParenthesis = s.chars().filter(c -> {
if (c == ')' && openingParenthesis.isEmpty())
return true;
if (c == '(')
openingParenthesis.push(c);
else
openingParenthesis.pop();
return false;
}).count();
return missingOpeningParenthesis + openingParenthesis.size();
}
public static void main(String[] args) {
System.out.println(solve("((()))")); // 0
System.out.println(solve("()()()")); // 0
System.out.println(solve("()())()")); // 1
System.out.println(solve("()()))()")); // 2
System.out.println(solve("()()))(())(")); // 3
System.out.println(solve(")(")); // 2
}
}