올바른 괄호


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

문제보기
Alt text

소스코드

import java.util.Stack;

class Solution {
     boolean  solution(String s) {
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            char parentheses = s.charAt(i);

            if ('(' == parentheses) {
                stack.add(parentheses);
            } else {
                if (stack.isEmpty()) return false;

                stack.pop();
            }
        }

        if (stack.isEmpty()) return true;
        return false;
    }
}