Advertisement
mirozspace

BalBrackets

Feb 1st, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Pr6BalancedBrackets {
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  8.         int lines = Integer.parseInt(reader.readLine());
  9.         int count = 0;
  10.         int flag1 = 0;
  11.         int flag2 = 0;
  12.  
  13.         for (int i = 0; i < lines; i++) {
  14.             String oneLine = reader.readLine();
  15.             char symbol = oneLine.charAt(0);
  16.             if (symbol == 40) {
  17.                 count++;
  18.                 flag1++;
  19.                 if (flag1 == 2) {
  20.                     System.out.println("UNBALANCED");
  21.                     return;
  22.                 }
  23.                 if (flag2 > 0) flag2--;
  24.             } else if (symbol == 41) {
  25.                 flag2++;
  26.                 count++;
  27.                 if (flag2 == 2) {
  28.                     System.out.println("UNBALANCED");
  29.                     return;
  30.                 }
  31.                 if (flag1 > 0) flag1--;
  32.             }
  33.         }
  34.         reader.close();
  35.         if ((flag1 != flag2) && (count % 2 == 0)) {
  36.             System.out.println("BALANCED");
  37.         } else {
  38.             System.out.println("UNBALANCED");
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement