Post

[백준/코틀린] 9012번: 괄호

실버 4

링크

9012번: 괄호

풀이

Stack을 사용하여 괄호의 짝이 올바른지 검사합니다.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
fun main() = repeat(readln().toInt()) {
    val stack = ArrayDeque<Char>()

    readln().forEach {
        if (it == ')' && stack.lastOrNull() == '(') stack.removeLast()
        else stack.addLast(it)
    }

    println(
        if (stack.isEmpty()) "YES"
        else "NO"
    )
}

This post is licensed under CC BY 4.0 by the author.