You are on page 1of 1

#include <iostream>

#include<stack>
using namespace std;

class balance {
public:
bool isValid(string A) {
stack<char> s;

for(int i=0;i<A.size();i++){

if(A[i]=='(' || A[i]=='{' || A[i]=='['){


s.push(A[i]);
} else{

if(s.empty()) return false; /// imp otherwise run time erro


else if(A[i]==')'){
if(s.top() == '(') s.pop();
else return false;
}else if(A[i]=='}'){
if(s.top() == '{') s.pop();
else return false;
}else if(A[i]==']'){
if(!s.empty() && s.top() == '[') s.pop();
else return false;
}

if(s.empty()) return true;

return false;
}
};
int main()
{
balance b1;
string s1;
cout<<"Enter the expression"<<endl;
cin>>s1;
cout<<b1.isValid(s1)<<endl;
}

You might also like