Algorythms
백준 9012번 풀이
by 준형코딩
2022. 2. 22.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Stack;
public class baekjoon9021 {
public static void main(String[]args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(br.readLine());
Stack<String> stack = new Stack<>();
boolean check = false;
for(int i = 0;i<n;i++){
String s = br.readLine();
for(int i1=0;i1<s.length();i1++){
String s2 = Character.toString(s.charAt(i1));
if(s2.equals("(")){
stack.push("(");
//System.out.println("stack pushed");
}else{
try{
stack.pop();
// System.out.println("stack poped");
}catch(Exception e){
//System.out.println("stack null");
check = true;
break;
}
//System.out.println(stack.size());
}
}
if(stack.size()>0||check==true){
System.out.println("NO");
}else{
System.out.println("YES");
}
stack.clear();
check=false;
//System.out.println(stack.size());
}
}
}