class Test20 { class Solution { fun isValid(s: String): Boolean { val stack = ArrayDeque(listOf()) for (c in s) { when(c){ ')' ->{ if (stack.isEmpty() || stack.last() != '('){ return false }else{ stack.removeLast() } } '}' ->{ if (stack.isEmpty() || stack.last() != '{'){ return false }else{ stack.removeLast() } } ']' ->{ if (stack.isEmpty() || stack.last() != '['){ return false }else{ stack.removeLast() } } else -> { stack.addLast(c) } } } return stack.isEmpty() } } fun test(): Unit { println(Solution().isValid("()")) println(Solution().isValid("()[]{}")) println(Solution().isValid("(]")) println(Solution().isValid("]")) } }