Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.scalefocus;
- import java.math.BigDecimal;
- import java.text.DecimalFormat;
- import java.util.*;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = Integer.parseInt(sc.nextLine());
- boolean isBalanced = true;
- boolean openingBracket = false;
- boolean closingBracket = false;
- String input = "";
- for (int i = 0; i < n ; i++) {
- input = sc.nextLine();
- if (input.equals("(") ) {
- if (!openingBracket) {
- openingBracket = true;
- } else {
- isBalanced = false;
- break;
- }
- } else if (input.equals(")") ){
- if (openingBracket) {
- openingBracket = false;
- } else {
- isBalanced = false;
- break;
- }
- }
- }
- if (isBalanced && !openingBracket) {
- System.out.println("BALANCED");
- } else {
- System.out.println("UNBALANCED");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement