본문 바로가기
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());



        }




    }
}

'Algorythms' 카테고리의 다른 글

백준 1092번 c++ 풀이  (0) 2022.08.31
백준 1967 C++ 준코  (0) 2022.08.08
백준 10757 자바 풀이  (0) 2022.03.07
백준 2164번 자바 풀이  (0) 2022.03.01
백준 10828풀이 (스택)  (0) 2022.02.22