fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static class stack
  8. {
  9. char arr[] = new char[10];
  10. int current;
  11. stack(){current = 0;};
  12. void push(char x)
  13. {
  14. arr[current] = x;
  15. current++;
  16. }
  17. void pop()
  18. {
  19. current--;
  20. }
  21. char back()
  22. {
  23. return arr[current-1];
  24. }
  25. int size()
  26. {
  27. return current;
  28. }
  29. };
  30. public static void main (String[] args) throws java.lang.Exception
  31. {
  32. String brace;
  33. Scanner input = new Scanner(System.in);
  34. brace = input.nextLine();
  35. stack x = new stack();
  36. char a = brace.charAt(0);
  37. if(a == '(' || a == '{' || a== '[')
  38. {
  39. for(int i=0;i<brace.length();i++)
  40. {
  41. a = brace.charAt(i);
  42. if(a == '(' || a == '{' || a== '[')
  43. x.push(a);
  44. else if(Math.abs(a - x.back()) < 3 && x.size() != 0)
  45. {
  46. x.pop();
  47. } else
  48. {
  49. System.out.println("No");
  50. System.exit(0);
  51. }
  52. }
  53. if(x.size() == 0)
  54. {
  55. System.out.println("Yes");
  56. } else System.out.println("No");
  57. }
  58. else
  59. {
  60. System.out.println("No");
  61. System.exit(0);
  62. }
  63. }
  64. }
Success #stdin #stdout 0.05s 4640768KB
stdin
({})
stdout
Yes