백준 9012번(Java)

박은지·2025년 2월 12일

백준

목록 보기
28/89
post-thumbnail

import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		int T = Integer.parseInt(br.readLine());
		
		for(int i=0; i<T; i++) {
			sb.append(solve(br.readLine())).append('\n');
		}
		System.out.println(sb);
	}
	
	public static String solve(String s) {
		Stack<Character> stack = new Stack<>();
		
		for(int i=0; i<s.length(); i++) {
			char c = s.charAt(i);
			
			if(c == '(') {
				stack.push(c);
			} else if(stack.empty()) {
				return "NO";
			} else {
				stack.pop();
			}
		}
		
		if(stack.empty()) {
			return "YES";
		} else {
			return "NO";		
		}
	}
}
profile
백엔드 개발자가 되고싶은 eunzi😊

0개의 댓글