-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon9012.java
49 lines (42 loc) · 1.22 KB
/
Baekjoon9012.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
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
/**
* https://www.acmicpc.net/problem/9012
* 백준 9012번 괄호
*/
public class Baekjoon9012 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
solution(br.readLine());
}
}
private static void solution(String input) {
if(input.charAt(0) == ')' || input.charAt(input.length()-1) == '('){
System.out.println("NO");
return;
}
Stack<Character> stack = new Stack<>();
for (char c : input.toCharArray()) {
if (c == '(') {
stack.push(c);
continue;
}
if (c == ')' && !stack.isEmpty()) {
stack.pop();
} else {
System.out.println("NO");
return;
}
}
if(!stack.isEmpty()){
System.out.println("NO");
return;
}
System.out.println("YES");
}
}