🐣 알고리즘 삐약/💻 백준 삐약

48 삐약 : 백준 9012| 괄호 [바킹독 문제 풀이|Stack|JAVA]

우주수첩 2023. 12. 20. 11:15
728x90

https://www.acmicpc.net/problem/9012

 

9012번: 괄호

괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고

www.acmicpc.net

 

package BKD_0x8_Stack_Application;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class BOJ_9012 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());
        Stack<Character> stack = new Stack<>();

        for(int i=0;i<N;i++){
            char[] arr = br.readLine().toCharArray();
            for(char c : arr){
                switch (c){
                    case '(': stack.push(c); break;
                    case ')':
                        if(!stack.isEmpty()&&stack.peek()=='(') stack.pop();
                        else stack.push(c);
                        break;
                }
            }
            if(stack.isEmpty()) System.out.println("YES");
            else System.out.println("NO");

            stack.clear();
        }
    }
}

 

 

728x90