forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
64 lines (55 loc) · 1.99 KB
/
Solution.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
64
//Problem: https://www.hackerrank.com/challenges/balanced-brackets
//Java 8
/*
First we know that for every opening sequence we need a closing sequence
Because they are in order we need some way to store them in order
For this we could use a stack or possibly a linked list
I think it would be better to use a stack since it offers us the ability
to quickly add remove, and check without messy code
So using a stack all we need to do is to keep track of the
order in which we are expecting our exit sequences
If that order is ever broken then we know the answer is no
edge cases:
if we have an extra pening sequence and nothing on the stack
make sure not to pop or peek instead if empty answer is no
if we reach the end of sequence and have an empty stack then
answer is yes, otherwise answer is no
*/
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");
}
}
}