• Home
  • About
    • Moon photo

      Moon

      개발자는 자고 싶다.

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Posts
    • All Posts
    • All Tags
  • Projects

백준 - 9012 괄호

07 Jun 2022

Reading time ~1 minute

문제

9012 괄호

screencapture

답

kotlin code

import java.util.*

fun main() {
    q9012()
}

fun q9012() = with(Scanner(System.`in`)) {
    val inputs = List(nextInt()) {next()}
    for(input in inputs) {
        var isVps = true
        val stack = Stack<Char>()
        for (char in input.toCharArray()) when (char) {
            '(' -> stack.push(char)
            ')' -> {
                if (stack.isEmpty()) {
                    isVps = false
                    break
                }
                stack.pop()
            }
        }
        if(stack.isNotEmpty()) isVps = false
        println(if(isVps) "YES" else "NO")
    }
}


baekjoonkotlin코틀린백준실버자료 구조문자열스택 Share Tweet +1