[백준] 9012번 문제
문제
문제출처 : Baekjoon
풀이
import java.util.*;
public class Main {
public String solution(String str){
String answer = "YES";
Stack<Character> stack = new Stack<>();
for(char x : str.toCharArray()){
if(x == '('){
stack.push(x);
}else{
if(stack.isEmpty()){
return "NO";
}else{
stack.pop();
}
}
}
if(!stack.isEmpty()){
return "NO";
}
return answer;
}
public static void main(String[] args) {
Main function = new Main();
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for(int i = 0; i < num; i++){
System.out.println(function.solution(sc.next()));
}
}
}
회고
- 여러 번 풀어보기!!!!