-
Notifications
You must be signed in to change notification settings - Fork 1
/
Parenthesis_Checker.java
44 lines (41 loc) · 1.12 KB
/
Parenthesis_Checker.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
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t--!=0){
Stack<Character> s = new Stack<>();
String str = scan.next();
int flag = 0;
for(char c : str.toCharArray()){
if(c=='{' || c=='(' || c=='['){
s.push(c);
}else {
if(s.isEmpty()){
System.out.println("not balanced");
flag = 1;
break;
}else{
if((s.peek()=='{' && c == '}') || (s.peek()=='[' && c==']')||(s.peek()=='(' && c == ')')){
s.pop();
}else{
System.out.println("not balanced");
flag =1;
break;
}
}
}
}
if(flag==0)
if(s.isEmpty()){
System.out.println("balanced");
}else{
System.out.println("not balanced");
}
}
}
}