-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balanced Brackets.java
43 lines (38 loc) · 1.17 KB
/
Balanced Brackets.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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
tests:
for(int a0 = 0; a0 < t; a0++){
String s = in.next();
Stack<Character> stack = new Stack<>();
for(char c : s.toCharArray())
{
if(c == '(')
stack.push(')');
else if(c == '{')
stack.push('}');
else if(c == '[')
stack.push(']');
else{
if( stack.isEmpty() || c != stack.peek()){
System.out.println("NO");
continue tests;
}
else{
stack.pop();
}
}
}
if(stack.isEmpty())
System.out.println("YES");
else
System.out.println("NO");
}
}
}