Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises.MoreMore;
- import java.util.Scanner;
- public class BalancedBrackets {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- //Входните данни са инт число.
- //Цикъл до rotations и вътрешна проверка на всеки чар.
- //Следене на булевите променливи с проверки.
- //Принтиране
- int rotations = Integer.parseInt(scanner.nextLine());
- boolean opening = false;
- boolean closing = false;
- boolean balanced = false;
- for (int i = 1; i <= rotations ; i++) {
- String input = scanner.nextLine();
- if (input.contains(")")){
- if (!opening){
- balanced = true;
- } else {
- closing = true;
- opening = false;
- }
- }
- if (input.contains("(")){
- if (opening){
- balanced = true;
- } else {
- if (closing){
- closing = false;
- }
- opening = true;
- }
- }
- }
- if (balanced){
- System.out.println("UNBALANCED");
- } else {
- System.out.println("BALANCED");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment