forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidParenthesis.cpp
92 lines (83 loc) · 2.11 KB
/
ValidParenthesis.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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define ll long long int
#define ld long double
#define mod 1000000007
#define endl "\n"
#define vi vector<ll>
#define vs vector<string>
#define pii pair<ll, ll>
#define ump unordered_map
#define mp map
#define pq_max priority_queue<ll>
#define pq_min priority_queue<ll,vi,greater<ll>
#define ff first
#define ss second
#define mid(l,r) (l+(r-l)/2)
#define loop(i,a,b) for(int i=(a); i <=(b);i++)
#define looprev(i,a,b) for(int i=(a); i >=(b);i--)
#define clr(val) memset(val,0,sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
bool Balanced(string exp){
stack <char> st;
char p;
for(int i = 0; i < exp.length(); i++){
if (exp[i] == '(' || exp[i] == '[' || exp[i] == '{'){
st.push(exp[i]);
continue;
}
if (st.empty()){
return false;
}
switch (exp[i])
{
case ')':
p = st.top();
st.pop();
if (p == '{' || p == '['){
return false;
}
break;
case ']':
p = st.top();
st.pop();
if (p == '{' || p == '('){
return false;
}
break;
case '}':
p = st.top();
st.pop();
if (p == '[' || p == '('){
return false;
}
break;
}
}
return (st.empty());
}
int main()
{
string s; //()[]{} ()[}
cin>>s;
if (Balanced(s)){
cout<<"Balanced";
}
else{
cout<<"Not Balanced";
}
return 0;
}