본문 바로가기
Algorithm & SQL/BAEKJOON

백준 JAVA 9012 괄호

by YoonJong 2022. 7. 19.
728x90

package BAEKJOON;

import java.util.Scanner;
import java.util.Stack;

public class NO9012 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        for (int i = 0; i < n; i++) {
            Stack<Character> st = new Stack<>();
            String str = sc.next();

            for (int j = 0; j < str.length(); j++) {
                if (str.charAt(j) == '(') {
                    st.push(str.charAt(j));
                } else {
                    if (st.empty()) {
                        st.push(str.charAt(j));
                        break;
                    } else {
                        st.pop();
                    }
                }
            }
            if (st.isEmpty()) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
}
728x90

'Algorithm & SQL > BAEKJOON' 카테고리의 다른 글

백준 JAVA 11866 요세푸스 문제0  (0) 2022.07.21
백준 JAVA 1026 보물  (0) 2022.07.19
백준 JAVA 1065 한수  (0) 2022.07.19
백준 JAVA 1920 수 찾기  (0) 2022.07.17
백준 JAVA 2163 초콜릿 자르기  (0) 2022.07.16

댓글