borovaneca

BalancedBrackets

Dec 7th, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises.MoreMore;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class BalancedBrackets {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         //Входните данни са инт число.
  10.         //Цикъл до rotations и вътрешна проверка на всеки чар.
  11.         //Следене на булевите променливи с проверки.
  12.         //Принтиране
  13.  
  14.         int rotations = Integer.parseInt(scanner.nextLine());
  15.         boolean opening = false;
  16.         boolean closing = false;
  17.         boolean balanced = false;
  18.  
  19.         for (int i = 1; i <= rotations ; i++) {
  20.  
  21.             String input = scanner.nextLine();
  22.             if (input.contains(")")){
  23.                 if (!opening){
  24.                     balanced = true;
  25.                 } else {
  26.                     closing = true;
  27.                     opening = false;
  28.                 }
  29.             }
  30.             if (input.contains("(")){
  31.                 if (opening){
  32.                     balanced = true;
  33.                 } else {
  34.                     if (closing){
  35.                         closing = false;
  36.                     }
  37.                     opening = true;
  38.                 }
  39.             }
  40.         }
  41.         if (balanced){
  42.             System.out.println("UNBALANCED");
  43.         } else {
  44.             System.out.println("BALANCED");
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment