I have written code for parenthesis checker using stack in c++ ,it is working perfectly fine for all test cases individually but when i perform test case simultaneously in one go then it is giving error of segmentation fault.
using namespace std;
int main()
{
//code
int t;
cin>>t;
while(t--){
string str;
cin>>str;
stack<char>box;
int len = str.size();
box.push(str[0]);
for(int i =1;i<len;i++){
char temp = str[i];
char tp = box.top();
if(( (tp=='{' && temp=='}') || ( tp=='[' && temp==']') || (tp=='(' && temp==')') )&&(!box.empty()) ){
// cout<<"here";
box.pop();
}else{
//cout<<temp;
box.push(temp);
}
}
if(box.empty()==true) cout<<"balanced"<<endl;
else cout<<"not balanced"<<endl;
}
return 0;
``` }
Aucun commentaire:
Enregistrer un commentaire