-
Notifications
You must be signed in to change notification settings - Fork 1
/
Baekjoon2504.java
57 lines (46 loc) · 1.54 KB
/
Baekjoon2504.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
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
/**
* https://www.acmicpc.net/problem/2504
* 백준 2504번 괄호의 값
*/
public class Baekjoon2504 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
String input = br.readLine();
if (input.charAt(0) == ')' || input.charAt(0) == ']' || input.charAt(input.length() - 1) == '(' || input.charAt(input.length() - 1) == '[') {
System.out.println(0);
return;
}
Stack<Character> stack = new Stack();
//가능한지 확인
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == '(' || c == '[') stack.push(c);
else {
if (c == ')' && !stack.isEmpty()) {
if (stack.peek() == '(') {
stack.pop();
} else {
System.out.println(0);
return;
}
} else if (c == ']' && !stack.isEmpty()) {
if (stack.peek() == '[') {
stack.pop();
} else {
System.out.println(0);
return;
}
}
}
}
if (!stack.isEmpty()) {
System.out.println(0);
return;
}
}
}