-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ10799.java
More file actions
48 lines (43 loc) · 1.32 KB
/
Copy pathJ10799.java
File metadata and controls
48 lines (43 loc) · 1.32 KB
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
package TIL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
public class J10799 {
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String[] input = buffer.readLine().split("");
LinkedList<Character> stack = new LinkedList<>();
int size = input.length;
int[] map = new int[size];
int idx = 1;
char before = '(';
int ans = 0;
stack.push(input[0].charAt(0));
map[0] = 1;
while(size>idx){
char c = input[idx].charAt(0);
if(c == '('){
map[idx] = map[idx-1]+1;
stack.push(c);
before = '(';
}
else if (c == ')') {
if(before=='('){
before = ')';
map[idx] = map[idx-1] -1;
stack.pop();
ans += map[idx];
map[idx-1] = 0;
}else{
before = ')';
stack.pop();
ans +=1;
map[idx] = map[idx-1] -1;
}
}
idx++;
}
System.out.println(ans);
}
}