-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparenthesis_checker.cpp
More file actions
49 lines (43 loc) · 903 Bytes
/
parenthesis_checker.cpp
File metadata and controls
49 lines (43 loc) · 903 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
#include <bits/stdc++.h>
using namespace std;
bool match(char a,char b)
{ if(a=='{' && b=='}')
return true;
if(a=='(' && b==')')
return true;
if(a=='[' && b==']')
return true;
return false;
}
bool balanced(string exp)
{ stack<char> st;
int len = exp.length();
for(int i=0;i<len;i++)
{ if(exp[i]=='{' || exp[i]=='('||exp[i]=='[')
st.push(exp[i]);
else
{ if(st.empty())
return false;
char c = st.top();
if(match(c,exp[i]))
{
st.pop();
}
else{
return false;
}
}
}
if(st.empty())
return true;
return false;
}
int main() {
string str;
cin>>str;
if(balanced(str))
cout<<"balanced"<<endl;
else
cout<<"not balanced"<<endl;
return 0;
}