Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class matchingBracket {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int cycles = Integer.parseInt(scanner.nextLine());
- int countOpenBrackets = 0;
- boolean balanced = true;
- while (cycles-- > 0) {
- String input = scanner.nextLine();
- if (input.equals("(")) {
- countOpenBrackets++;
- } else if (input.equals(")")) {
- countOpenBrackets--;
- if (countOpenBrackets < 0) {
- balanced = false;
- }
- }
- }
- if (countOpenBrackets != 0) {
- balanced = false;
- }
- if(balanced){
- System.out.println("BALANCED");
- } else {
- System.out.println("UNBALANCED");
- }
- // System.out.println(balanced ? "BALANCED" : "UNBALANCED");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement