Advertisement
desislava_topuzakova

Untitled

Jan 11th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. package L02_DataTypes_Variables_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PME06_BalancedBrackets {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. byte n = Byte.parseByte(scanner.nextLine());
  10.  
  11. byte openingCount = 0;
  12. byte closingCount = 0;
  13. for (byte i = 1; i <= n; i++) {
  14. String input = scanner.nextLine();
  15. if (input.equals("(")) {
  16. openingCount++;
  17. }
  18. else if (input.equals(")")) {
  19. closingCount++;
  20. if (openingCount - closingCount != 0) {
  21. System.out.println("UNBALANCED");
  22. return;
  23. }
  24. }
  25. }
  26.  
  27. if (openingCount==closingCount) {
  28. System.out.println("BALANCED");
  29. }
  30. else {
  31. System.out.println("UNBALANCED");
  32. }
  33.  
  34.  
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement