9012번: 괄호
괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고
www.acmicpc.net
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Stack<Character> stack = new Stack<>();
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
String str = br.readLine();
if (str.length() % 2 == 1) {
System.out.println("NO");
continue;
}
for (int j = 0; j < str.length(); j++) {
if (!stack.isEmpty() && stack.peek() == '(' && str.charAt(j) == ')') {
stack.pop();
} else {
stack.push(str.charAt(j));
}
}
System.out.println(stack.isEmpty() ? "YES" : "NO");
stack.clear();
}
}
}
'Java > AlgorithmPS' 카테고리의 다른 글
백준) 균형잡힌 세상 (4949번) (0) | 2023.10.04 |
---|---|
백준) 도키도키 간식드리미 (12789번) (0) | 2023.09.27 |
백준) 좋은 단어(3986번) - 복습 필요 (0) | 2023.09.23 |
백준) 단어 뒤집기2(17413번) (0) | 2023.09.23 |
백준) 과제는 끝나지 않아(17952번) (0) | 2023.09.23 |