-
Notifications
You must be signed in to change notification settings - Fork 80
/
BalancedBrackets.java
63 lines (49 loc) · 1.6 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
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.*;
import java.util.*;
public class Solution {
private char smallOpen = '(';
private char smallClose = ')';
private char midOpen = '[';
private char midClose = ']';
private char bigOpen = '{';
private char bigClose = '}';
List<Character> oList = Arrays.asList(smallOpen, midOpen, bigOpen);
Map<Character, Character> openToCloseMap = new HashMap<>();
{
openToCloseMap.put(smallOpen, smallClose);
openToCloseMap.put(midOpen, midClose);
openToCloseMap.put(bigOpen, bigClose);
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Solution sol = new Solution();
sol.process();
}
public void process() {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
for (int i = 0; i < N; i++) {
String s = sc.nextLine();
System.out.println(process(s)? "YES" : "NO");
}
}
public boolean process(String s) {
Deque<Character> deque = new ArrayDeque<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (oList.contains(c)) {
deque.push(c);
} else {
if (deque.isEmpty()) {
return false;
}
char pop = deque.pop();
if (openToCloseMap.get(pop) != c) {
return false;
}
}
}
return deque.isEmpty();
}
}