백준9012: 괄호
문제
https://www.acmicpc.net/problem/9012
문제 풀이
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
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack<String> stack = new Stack<>();
boolean VPS = true;
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
String str = sc.next();
for(String s : str.split("")){
if(s.equals("(")){
stack.push(s);
} else {
if(stack.isEmpty()){
VPS = false;
break;
} else{
stack.pop();
}
}
}
if (VPS && stack.isEmpty()) {
System.out.println("YES");
} else {
System.out.println("NO");
}
VPS=true;
stack.clear();
}
}
}
댓글남기기