[백준] 9012 - 괄호

zzenee·2022년 5월 20일
0

Algorithm&Coding-test

목록 보기
13/30
post-thumbnail

Problem

Code

import java.util.*;

class Main {
	static int n;
	static String[] answer;

	public String VPS (String str) {
		char[] tmp = str.toCharArray();
		Stack<Character> stack = new Stack<>();
		for (int i=0; i<tmp.length; i++) {
			if (stack.isEmpty()) stack.push(tmp[i]);
			else if (stack.peek() == '(' && tmp[i] == ')') stack.pop();
			else stack.push(tmp[i]);
		}
		if (!stack.isEmpty()) return "NO";
		else return "YES";
	}

  	public static void main(String[] args) {
		Main ex = new Main();
		Scanner scan = new Scanner(System.in);
		n = scan.nextInt();
		answer = new String[n];
		for (int i=0; i<n; i++) {
			answer[i] = ex.VPS(scan.next());
		}
		for (String ans : answer) {
			System.out.println(ans);
		}
  	}
}

Result

profile
꾸준히

0개의 댓글