문제
https://www.acmicpc.net/problem/9012
9012번: 괄호
괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고
www.acmicpc.net
코드
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int task = Integer.parseInt(br.readLine());
for(int t=0;t<task;t++){
String str = br.readLine();
Stack<Character> stack = new Stack<>();
for(int i=0;i<str.length();i++) {
char ch = str.charAt(i);
if(ch == '(')
stack.add(ch);
else {
if(!stack.isEmpty() && stack.peek() == '(')
stack.pop();
else
stack.add(ch);
}
}
if(!stack.isEmpty()) {
System.out.println("NO");
}
else {
System.out.println("YES");
}
}
}
}
문제
https://www.acmicpc.net/problem/9012
9012번: 괄호
괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고
www.acmicpc.net
코드
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int task = Integer.parseInt(br.readLine()); for(int t=0;t<task;t++){ String str = br.readLine(); Stack<Character> stack = new Stack<>(); for(int i=0;i<str.length();i++) { char ch = str.charAt(i); if(ch == '(') stack.add(ch); else { if(!stack.isEmpty() && stack.peek() == '(') stack.pop(); else stack.add(ch); } } if(!stack.isEmpty()) { System.out.println("NO"); } else { System.out.println("YES"); } } } }