-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBRACKETS.java
61 lines (49 loc) · 1.06 KB
/
BRACKETS.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package he;
import java.util.Scanner;
import java.util.Stack;
public class BRACKETS {
private static Scanner s;
public static void main(String[] args) {
s = new Scanner(System.in);
String str= s.nextLine();
int t= s.nextInt();
// ( removed
// ) removed
int fc;
if(t==1) {
Stack<Character> st=new Stack<>();
int count=0;
fc=0;
for(int i=str.length()-1; i>=0; i--) {
if(str.charAt(i)==')') {
st.push(str.charAt(i));
count++;
}
else if(str.charAt(i)=='(')
st.pop();
if(st.isEmpty()) {
fc+=count*2;
count=0;
}
}
}
else {
Stack<Character> st=new Stack<>();
int count=0;
fc=0;
for(int i=0; i<str.length(); i++) {
if(str.charAt(i)=='(') {
st.push(str.charAt(i));
count++;
}
else if(str.charAt(i)==')')
st.pop();
if(st.isEmpty()) {
fc+=count*2;
count=0;
}
}
}
System.out.println((str.length()+1 - fc)/2);
}
}