-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balanced_Brackets.cpp
242 lines (237 loc) · 3.29 KB
/
Balanced_Brackets.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include<iostream>
#include<string>
//#include<stack>
using namespace std;
template <class T>
class stack
{
int capacity;
int top;
T* arr;
public:
stack(int c)
{
capacity=c;
top=-1;
arr=new T[c];
}
void push(T x)
{
if(top>=capacity-1)
{
cout<<"overflow\n";
return;
}
arr[++top]=x;
}
void pop()
{
if(top==-1)
{
cout<<"underflow\n";
return;
}
top--;
}
T topp()
{
if(top==-1)
{
cout<<"underflow\n";
return(0);
}
return(arr[top]);
}
int isfull()
{
if(top>=capacity-1)
return(1);
else
return 0;
}
int size()
{
return(top+1);
}
int isempty()
{
if(top==-1)
return 1;
else
return 0;
}
};
/*int main()
{
stack <char> s1(5);
for(int i=0;i<5;i++)
{
int ele;
cin>>ele;
s1.push(ele);
}
s1.push(100);
s1.push(999);
for(int i=0;i<5;i++)
{
cout<<s1.pop()<<" ";
}
cout<<endl;
int t=s1.pop();
cout<<s1.isempty()<<endl;
cout<<s1.isfull()<<endl;
}
*/
/*class stack;
class node
{
friend class stack;
int data;
node* next;
public:
node(int x)
{
data=x;
next=NULL;
}
};
class stack
{
node* head;
int capacity;
int size;
public:
stack(int c)
{
head=NULL;
capacity=c;
size=0;
}
void push(int ele)
{
if(size==capacity)
{
cout<<"overflow\n";
return;
}
node* nn=new node(ele);
size++;
nn->next=head;
head=nn;
}
int pop()
{
if(head==NULL)
{
cout<<"underflow\n";
return(0);
}
int res=head->data;
node* temp=head;
head=head->next;
delete temp;
size--;
return(res);
}
int isempty()
{
if(head==NULL)
return 1;
else
return 0;
}
int isfull()
{
if(size==capacity)
return 1;
else
return 0;
}
int getsize()
{
return(size);
}
};
stack copy(stack& ip)
{
stack temp(5),res(5);
while(ip.isempty()==0)
{
int ele=ip.pop();
temp.push(ele);
}
while(temp.isempty()==0)
{
int ele=temp.pop();
res.push(ele);
}
return(res);
}
void copy(stack &ip,stack &res)
{
if(ip.isempty()==1)
return;
int ele=ip.pop();
copy(ip,res);
res.push(ele);
}*/
int isbalanced(string s)
{
stack <char> s1(25);
for(int i=0;i<s.size();i++)
{
if(s[i]=='(' || s[i]=='{' || s[i]=='[')
{
s1.push(s[i]);
}
else
{
if(s1.isempty()==1)
{
return 0;
}
else if( s[i]==')' && s1.topp()=='(' || s[i]=='}' && s1.topp()=='{' || s[i]==']' && s1.topp()=='[' )
{
s1.pop();
}
else
{
return(0);
}
}
}
if(s1.isempty()==1)
{
return(1);
}
else
{
return(0);
}
}
/*int isbalanced(std::string s) {
std::stack<char> s1;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
s1.push(s[i]);
} else {
if (s1.empty()) {
return 0; // No corresponding opening bracket on the stack.
}
if ((s[i] == ')' && s1.top() == '(') ||
(s[i] == '}' && s1.top() == '{') ||
(s[i] == ']' && s1.top() == '[')) {
s1.pop();
} else {
return 0; // Mismatched closing bracket.
}
}
}
return s1.empty() ? 1 : 0; // Check if there are any unclosed opening brackets.
}*/
int main()
{
string s1="([([])]){({{{[[()]]}}})}";
int x=isbalanced(s1);
cout<<x<<endl;
}