You are viewing a single comment's thread. Return to all comments →
#include<iostream> #include<stack> using namespace std; bool isBalance(string s,int i=0) { stack<char>stackk; for(int i=0;i<s.length();i++) { if(s[i]=='[' || s[i]=='{' || s[i]=='(') { stackk.push(s[i]); } else if(s[i]==']' && stackk.top()!='[') { return false; } else if(s[i]==')' && stackk.top()!='(') { return false; } else if(s[i]=='}' && stackk.top()!='{') { return false; } else { stackk.pop(); } } return true; } int main() { int n; cin>>n; while(n--) { string s; cin>>s; if(isBalance(s)) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } } return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Stacks: Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
**Getting seg fault please someone help **