Advertisement
desislava_topuzakova

Untitled

Jan 29th, 2023
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. package com.scalefocus;
  2.  
  3. import java.math.BigDecimal;
  4. import java.text.DecimalFormat;
  5. import java.util.*;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Main {
  9.  
  10. public static void main(String[] args) {
  11. Scanner sc = new Scanner(System.in);
  12. int n = Integer.parseInt(sc.nextLine());
  13. boolean isBalanced = true;
  14. boolean openingBracket = false;
  15. boolean closingBracket = false;
  16. String input = "";
  17. for (int i = 0; i < n ; i++) {
  18. input = sc.nextLine();
  19. if (input.equals("(") ) {
  20. if (!openingBracket) {
  21. openingBracket = true;
  22. } else {
  23. isBalanced = false;
  24. break;
  25. }
  26.  
  27. } else if (input.equals(")") ){
  28. if (openingBracket) {
  29. openingBracket = false;
  30. } else {
  31. isBalanced = false;
  32. break;
  33. }
  34.  
  35. }
  36.  
  37. }
  38. if (isBalanced && !openingBracket) {
  39. System.out.println("BALANCED");
  40. } else {
  41. System.out.println("UNBALANCED");
  42. }
  43.  
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement