-
Notifications
You must be signed in to change notification settings - Fork 0
/
BalancedBrackets.java
50 lines (43 loc) · 1.2 KB
/
BalancedBrackets.java
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
43
44
45
46
47
48
49
50
import java.util.Scanner;
import java.util.Stack;
public class BalancedBrackets {
public static boolean isBalanced(String expression) {
Stack<Character> open = new Stack<>();
for(int i = 0 ; i<expression.length(); i++)
if(i<expression.length()-1&&ismatch(expression.charAt(i), expression.charAt(i+1)))
i++;
else if(isopen(expression.charAt(i)))
open.push(expression.charAt(i));
else{
if(open.empty()||!ismatch(open.pop(), expression.charAt(i)))
return false;
}
return open.empty();
}
static boolean ismatch(char a, char b){
if(a=='('&&b==')')
return true;
else if(a=='['&&b==']')
return true;
else if(a=='{'&&b=='}')
return true;
return false;
}
static boolean isopen(char a){
if(a=='('||a=='['||a=='{')
return true;
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
String[] expression = new String[t];
for (int a0 = 0; a0 < t; a0++) {
expression[a0] = in.next();
}
for (int a0 = 0; a0 < t; a0++) {
System.out.println( (isBalanced(expression[a0])) ? "YES" : "NO" );
}
in.close();
}
}