-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalanceBrackets.cpp
42 lines (40 loc) · 980 Bytes
/
BalanceBrackets.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
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main(){
string s;
cout<<"enter string = ";
cin>>s;
stack<char> st;
// int c;
for (char ch : s) {
if (ch == '(' || ch == '[' || ch == '{') {
st.push(ch);
}
// []{}
// ({[]})
else if (ch == ')' || ch == ']' || ch == '}') {
if (st.empty()) {
cout << "Not valid parenthesis" << endl;
return 0;
}
char top = st.top();
if ((ch == ')' && top == '(') || (ch == ']' && top == '[') || (ch == '}' && top == '{')) {
st.pop();
// c=c*2;
}
else {
cout << "Not valid parenthesis" << endl;
return 0;
}
}
}
if(st.empty()){
cout<<"valid parenthesis\n";
// cout<<c;
}
else{
cout<<"not valid parenthesis\n";
}
}