-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbalanced_paranthesis.cpp
More file actions
56 lines (51 loc) · 1019 Bytes
/
balanced_paranthesis.cpp
File metadata and controls
56 lines (51 loc) · 1019 Bytes
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
#include<iostream>
#include<stack>
#include<string>
using namespace std;
bool are_pair(char opening,char closing)
{
if(opening=='(' && closing==')')
{
return true;
}
else if(opening=='[' && closing==']')
{
return true;
}
else if(opening=='{' && closing=='}')
{
return true;
}
return false;
}
bool are_parenthesis_balanced(string exp)
{
stack <char>s;
for(int i=0;i<exp.length();i++)
{
if(exp[i]=='(' || exp[i]=='[' || exp[i]=='{')
{
s.push(exp[i]);
}
else if(exp[i]==')' || exp[i]==']' || exp[i]=='}')
{
if(s.empty() || !are_pair(s.top(),exp[i]))
{
return false;
}
else
s.pop();
}
}
return s.empty()?true:false;
}
int main()
{
string exp;
cin>>exp;
if(are_parenthesis_balanced(exp))
cout<<"Balanced\n";
else
cout<<"Unbalanced\n";
return 0;
}