-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparanthesisChecker.cpp
More file actions
67 lines (61 loc) · 1.11 KB
/
paranthesisChecker.cpp
File metadata and controls
67 lines (61 loc) · 1.11 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
#include<iostream>
using namespace std;
int top=-1;
void push(char a[],char data)
{
a[++top]=data;
}
char pop(char a[])
{
char s=a[top--];
// cout<<top<<" "<<s<<endl;
return s;
}
int main()
{
int t;
cin>>t;
while(t--){
top=-1;
int flag=-1;
char *arr = new char[100000];
char *arr2=new char[100000];
string s;
cin>>s;
char temp;
int i=-1;
do
{i++;
arr[i]=s[i];
}while(arr[i]!='\0');
for(int j=0;j<i;j++)
{
if(arr[j]=='{' || arr[j]=='[' || arr[j]=='(')
{
push(arr2,arr[j]);
// cout<<top<<" " <<arr2[top]<<endl;
}
else if (arr[j]=='}' || arr[j]==']' || arr[j]==')')
{
temp=pop(arr2);
// cout<<temp;
if(arr[j]=='}' && temp=='{')
continue;
else if(arr[j]==']' && temp=='[')
continue;
else if(arr[j]==')' && temp=='(')
continue;
else
{
//cout<<"not balanced"<<endl;
flag=1;
break;
}
}
}
if(flag!=1&&top==-1) cout<<"balanced"<<endl;
else
cout<<"not balanced";
}
return 0;
}