Guest User

Untitled

a guest
Jul 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Braces {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. String str = sc.nextLine();
  7. char[] braces = str.toCharArray();
  8.  
  9. int count1 = 0; // counter for ()
  10. int count2 = 0; // counter for {}
  11. int count3 = 0; // counter for []
  12.  
  13. boolean sup = true;
  14.  
  15. int i = 0;
  16. while(true) {
  17. if(braces[i] == '(') {
  18. count1++;
  19. }
  20. if(braces[i] == ')') {
  21. count1--;
  22. }
  23. if(braces[i] == '{') {
  24. count2++;
  25. }
  26. if(braces[i] == '}') {
  27. count2--;
  28. }
  29. if(braces[i] == '[') {
  30. count3++;
  31. }
  32. if(braces[i] == ']') {
  33. count3--;
  34. }
  35.  
  36. if (count1 < 0 || count2 < 0 || count3 < 0) {
  37. sup = false;
  38. }
  39.  
  40. i++;
  41.  
  42. if(i >= braces.length) break;
  43. }
  44.  
  45. if(sup) {
  46. System.out.println("Ok");
  47. }
  48. else {
  49. System.out.println("Not ok");
  50. }
  51. }
  52. }
Add Comment
Please, Sign In to add comment