📍 문제 설명
💡 접근
스택 기본문제
👩💻 코드
import java.util.*;
public class Main {
static int n, m, k, cnt, max, min, sum, count, r, c;
static String answer;
static char[][] board, clone, dis;
static boolean[][] visited;
static int[] ch, pm, combi, graph, temp;
static boolean flag = false;
static int[] dx = {-1, 0, 1, 0,}; //북동남서
static int[] dy = {0, 1, 0, -1};
static ArrayList<Integer> list = new ArrayList<>();
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
n = kb.nextInt();
kb.nextLine();
String[] s = new String[n];
for(int i = 0; i < n; i++) {
s[i] = kb.nextLine();
}
for(int i = 0; i < n; i++) {
System.out.println(T.solution(s[i]));
}
}
private String solution(String s) {
answer = "YES";
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '(') stack.push('(');
else {
//닫는괄호가 더 많을 때
if(stack.isEmpty()) {
answer = "NO";
break;
}else stack.pop();
}
}
//여는 괄호가 더 많을 때
if(!stack.isEmpty()) answer = "NO";
return answer;
}
}