fork download
  1. import java.util.Stack
  2.  
  3. fun main() {
  4. val br = System.`in`.bufferedReader()
  5. val parentheses = Stack<Char>()
  6. val brackets = Stack<Char>()
  7.  
  8. val sb = StringBuilder()
  9. while (true) {
  10. val sentence = br.readLine()
  11. if (sentence == ".") {
  12. break
  13. }
  14.  
  15. var balanced = true
  16. loop@ for (c in sentence) {
  17. when (c) {
  18. '(' -> parentheses.push(c)
  19. ')' -> {
  20. if (parentheses.isEmpty() || parentheses.peek() != '(') {
  21. balanced = false
  22. break@loop
  23. }
  24. parentheses.pop()
  25. }
  26. '[' -> brackets.push(c)
  27. ']' -> {
  28. if (brackets.isEmpty() || brackets.peek() != '(') {
  29. balanced = false
  30. break@loop
  31. }
  32. brackets.pop()
  33. }
  34. }
  35. }
  36.  
  37. sb.append(if (balanced) "yes" else "no").append("\n")
  38.  
  39. parentheses.clear()
  40. brackets.clear()
  41. }
  42.  
  43. println(sb)
  44. }
Success #stdin #stdout 0.08s 40124KB
stdin
So when I die (the [first] I will see in (heaven) is a score list).
[ first in ] ( first out ).
Half Moon tonight (At least it is better than no Moon at all].
A rope may form )( a trail in a maze.
Help( I[m being held prisoner in a fortune cookie factory)].
([ (([( [ ] ) ( ) (( ))] )) ]).
 .
.
stdout
no
no
no
no
no
no
yes