취업준비 - 코테 , 면접/알고리즘(코테) 공부
알고리즘입문(Java) - Stack,Queue 1.올바른괄호
kwohyuno
2023. 3. 20. 23:19

(풀이)
import java.util.*;
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";
stack.pop();
}
}
if(!stack.isEmpty()) return "NO";
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str=kb.next();
System.out.println(T.solution(str));
}
}