fork(1) download
  1. import java.util.*;
  2.  
  3. class B {
  4.  
  5. public void run() {
  6. Scanner sc = new Scanner(System.in);
  7. for (String s = sc.nextLine(); !s.equals("."); s = sc.nextLine())
  8. System.out.println(check(s) ? "yes" : "no");
  9. }
  10.  
  11. private boolean check(String s) {
  12. Stack<Character> st = new Stack<Character>();
  13. for (char c : s.toCharArray())
  14. if (c == '(' || c == '[')
  15. st.push(c);
  16. else if (c == ')') {
  17. if (st.isEmpty() || st.pop() != '(')
  18. return false;
  19. } else if (c == ']') {
  20. if (st.isEmpty() || st.pop() != '[')
  21. return false;
  22. }
  23. return st.isEmpty();
  24. }
  25.  
  26. public static void main(String[] args) throws Exception {
  27. new B().run();
  28. }
  29. }
Success #stdin #stdout 0.07s 213888KB
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
yes
yes
no
no
no
yes
yes