Advertisement
Guest User

Untitled

a guest
May 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Stack;
  3.  
  4. public class Zagradi {
  5.  
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. String s = sc.nextLine().trim();
  9. Stack<Character> stack = new Stack<Character>();
  10. char[] niza = s.toCharArray();
  11. int brojac = 0;
  12. char[] z = new char[2];
  13.  
  14.  
  15.  
  16. for(int i =0;i<niza.length;i++)
  17. {
  18. stack.push(niza[i]);
  19. }
  20.  
  21.  
  22. while(!stack.isEmpty())
  23. {
  24. char a = stack.pop();
  25. if(a == ')' || a == ']' || a == '}')
  26. {
  27. z[brojac] = a;
  28. brojac++;
  29. }
  30. if(a == '(' || a == '[' || a == '{')
  31. {
  32. if(z[brojac-1] == a)
  33. {
  34. brojac--;
  35. }
  36. else
  37. {
  38. System.out.println("false");
  39. return;
  40. }
  41. }
  42. }
  43. System.out.println("true");
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement