
1. 풀이
import java.io.*;
import java.util.*;
public class Main {
public static StringBuilder sb = new StringBuilder();
public static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int N = Integer.parseInt(bf.readLine());
for (int i = 0; i < N; i++) {
solve(N);
}
System.out.println(sb);
}
public static void solve(int N) throws IOException {
List<String> arr = new ArrayList<>();
arr = List.of(bf.readLine().split(""));
Stack<String> stack = new Stack<>();
for (int j = 0; j < arr.size(); j++) {
String now = arr.get(j);
if (now.equals("(")) {
stack.push(now);
} else {
if (stack.empty()) {
sb.append("NO");
sb.append("\n");
return;
} else if (stack.lastElement().equals("(")) {
stack.pop();
}
}
}
if (stack.empty()) {
sb.append("YES");
sb.append("\n");
return;
} else {
sb.append("NO");
sb.append("\n");
return;
}
}
}
'백준' 카테고리의 다른 글
백준 3015 오아시스 재결합 (파이썬) (0) | 2022.12.14 |
---|---|
백준 2504 괄호의 값 (파이썬) (0) | 2022.12.13 |
백준 6603 로또 (파이썬, 자바) (0) | 2022.12.12 |
백준 2477 참외밭 (파이썬, 자바) (0) | 2022.12.12 |
백준 17144 미세먼지 안녕! (파이썬) (0) | 2022.12.08 |