• Home
  • About
    • Moon photo

      Moon

      개발자는 자고 싶다.

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

백준 - 4949 균형잡힌 세상

07 Jun 2022

Reading time ~1 minute

문제

4949 균형잡힌 세상

screencapture

답

kotlin code

import java.util.*

fun main() {
    q4949()
}

fun q4949() = with(Scanner(System.`in`)) {
    val inputList = kotlin.collections.ArrayList<String>()
    while (true) {
        val input = nextLine()
        if(input == ".") break
        inputList.add(input)
    }
    inputList.forEach {
        val stack = Stack<Char>()
        var isVps = true
        for(ch in it) when(ch) {
            '(', '[' -> stack.push(ch)
            ')' -> if(stack.isEmpty() || stack.pop() != '(') {
                isVps = false
                break
            }
            ']' -> if(stack.isEmpty() || stack.pop() != '[') {
                isVps = false
                break
            }
        }
        println(if(isVps && stack.empty()) "yes" else "no")
    }
}


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