-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedBrackets.java
More file actions
42 lines (32 loc) · 1010 Bytes
/
Copy pathBalancedBrackets.java
File metadata and controls
42 lines (32 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Scanner;
public class BalancedBrackets {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
int countOpeming = 0;
int countClosing = 0;
boolean flag = false;
for (int i = 1; i <= n ; i++) {
String input = scanner.nextLine();
if(input.equals("(") ){
countOpeming++;
}else if(input.equals(")")){
countClosing ++;
}
if(countOpeming == countClosing ){
flag = false;
}
if(countOpeming == 2 || (countClosing - countOpeming !=0 )){
flag = true;
}
if(input.equals("((") || input.equals("))")){
flag = true;
}
}
if(flag){
System.out.println("UNBALANCED");
}else {
System.out.println("BALANCED");
}
}
}