[프로그래머스] 올바른 괄호

2020. 4. 4. 14:46프로그래머스/LEVEL 2

import java.util.*;

class Solution {
    boolean solution(String s) {
        boolean answer = true;
        Stack<Character> st = new Stack<>();
        
        for(int i=0 ; i<s.length(); i++){
            if(s.charAt(i) =='('){
                st.push(s.charAt(i));
            }else{
                if(st.isEmpty()){
                    return false;
                }
                else{
                    st.pop();
                }
            }
        }
        
        if(st.isEmpty()){
            answer = true;
        }else{
            answer = false;
        }
        
        return answer;
    }
}