-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathParenthesis_Checker.java
More file actions
97 lines (90 loc) · 2.02 KB
/
Parenthesis_Checker.java
File metadata and controls
97 lines (90 loc) · 2.02 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.*;
class Parenthesis_Checker
{
Scanner sc=new Scanner(System.in);
int found=1;
int max=1000;
int top=-1;
char[] stack=new char[max];
int push(char ch)
{
if(top==max-1)
{
System.out.println("Stack is Full");
}
else
{
stack[++top]=ch;
}
return(top);
}
int pop(char ch)
{
if(top==-1)
{
found=0;
}
else
{
if(stack[top] != ch)
{
top=9616;
}
else
{
top--;
}
}
return top;
}
void main()
{
Parenthesis_Checker obj=new Parenthesis_Checker();
Scanner sc=new Scanner(System.in);
int j;
char ch1;String str;
int t=sc.nextInt();
while(t-->0)
{
int found=1;
sc.nextLine();
str=sc.next();
int len=str.length();
int topi=-1;
for(int i=0;i<len;i++)
{
ch1=str.charAt(i);
if(ch1=='(' || ch1=='{' || ch1=='[')
{
topi = obj.push(ch1);
}
else if(ch1==')')
{
topi=obj.pop('(');
}
else if(ch1=='}')
{
topi = obj.pop('{');
}
else if(ch1==']')
{
topi = obj.pop('[');
}
if(topi==9616)
{
break;
}
}
if(topi!=-1)
{
System.out.println("Not Balanced");
}
else if(topi==-1)
{
System.out.println("Balanced");
}
found=1;
obj.top=-1;
}
}
}