Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Pr6BalancedBrackets {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int lines = Integer.parseInt(reader.readLine());
- int count = 0;
- int flag1 = 0;
- int flag2 = 0;
- for (int i = 0; i < lines; i++) {
- String oneLine = reader.readLine();
- char symbol = oneLine.charAt(0);
- if (symbol == 40) {
- count++;
- flag1++;
- if (flag1 == 2) {
- System.out.println("UNBALANCED");
- return;
- }
- if (flag2 > 0) flag2--;
- } else if (symbol == 41) {
- flag2++;
- count++;
- if (flag2 == 2) {
- System.out.println("UNBALANCED");
- return;
- }
- if (flag1 > 0) flag1--;
- }
- }
- reader.close();
- if ((flag1 != flag2) && (count % 2 == 0)) {
- System.out.println("BALANCED");
- } else {
- System.out.println("UNBALANCED");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement