문제

풀이

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

    for (char c : s.toCharArray()) {
        if (c == '(') {
            stack.push(c);
        } else if (stack.isEmpty()) {
            return false;
        } else {
            stack.pop();
        }
    }

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

다른 풀이

boolean solution(String s) {
    int count = 0;

    for (char c : s.toCharArray()) {
        if (c == '(') {
            count++;
        } else if (count == 0) {
            break;
        } else {
            count--;
        } 
    }

    return count == 0;
}