Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Pattern;
  3.  
  4. public class Solution {
  5. public static void main(String []argh)
  6. {
  7. Scanner sc = new Scanner(System.in);
  8. boolean flag= true; int count=0;String count1 = null;;
  9. while (sc.hasNext()) {
  10. String input=sc.next();
  11. //Complete the code
  12. if(input.length()==0)
  13. {
  14. flag= true;
  15. break;
  16. }
  17. else
  18. {
  19.  
  20. for(int i=0;i<input.length();i++)
  21. {
  22. // System.out.println("alp" + i);
  23. for(int j=i+1;j<input.length();j++){
  24. if(input.charAt(i)=='{')
  25. {
  26.  
  27. //System.out.println("a");
  28. if(input.charAt(j)=='}')
  29. {
  30.  
  31. count=count+2;;
  32. break;
  33. }
  34. else
  35. {
  36. //System.out.println("b");
  37. count1="shrey";
  38. // System.out.println("b1");
  39. }
  40. }
  41. else if(input.charAt(i)=='[')
  42. {
  43.  
  44. //System.out.println("a"+i);
  45. if(input.charAt(j)==']')
  46. {
  47.  
  48. count=count+2;;
  49. break;
  50. }
  51. else
  52. {
  53. count1="shrey";
  54. // System.out.println("b2");
  55. }
  56. }
  57. else if(input.charAt(i)=='(')
  58. {
  59.  
  60. if(input.charAt(j)==')')
  61. {
  62.  
  63. count=count+2;;
  64. break;
  65. }
  66. else
  67. {
  68. count1="shrey";
  69. // System.out.println("b3" + count1);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. if(count%2==0 && input.length()%2==0)
  76. {
  77. if(!(input.charAt(input.length()-1)=='{' || input.charAt(input.length()-1)=='[' || input.charAt(input.length()-1)=='(')){
  78. //System.out.println(input.charAt(input.length()-1));
  79. flag=true;
  80. if((Pattern.matches("[{]*",input)&&(!Pattern.matches("[}]*",input))))
  81. {
  82. flag= false;
  83. }
  84. else if((Pattern.matches("[}]*",input)&&(!Pattern.matches("[{]*",input))))
  85. {
  86. flag= false;
  87. }
  88. else if((Pattern.matches("[)]*",input)&&(!Pattern.matches("[(]*",input))))
  89. {
  90. flag= false;
  91. }
  92. else if((Pattern.matches("[(]*",input)&&(!Pattern.matches("[)]*",input))))
  93. {
  94. flag= false;
  95. }
  96. }
  97. else
  98. {
  99. //System.out.println("b");
  100. flag=false;
  101. }
  102. }
  103.  
  104. else
  105. {
  106. flag= false;
  107. }
  108. System.out.println(flag);
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement