You are viewing a single comment's thread. Return to all comments →
my c++ solution taking care of special cases
#include<iostream> #include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; while(n!=0){ string a; cin>>a; stack <char> s; int i,flag=0; for(i=0;i<a.length();i++){ switch(a[i]){ case '{': case '[': case '(': s.push(a[i]); break; case '}': if(s.empty()||s.top()!='{'){ cout<<"NO"<<endl; flag=1; goto exit; } s.pop(); break; case ']': if(s.empty()||s.top()!='['){ cout<<"NO"<<endl; flag=1; goto exit; } s.pop(); break; case ')': if(s.empty()||s.top()!='('){ cout<<"NO"<<endl; flag=1; goto exit; } s.pop(); break; } } exit: if(s.empty()&&!flag) cout<<"YES"<<endl; if(!flag&&!s.empty()) cout<<"NO"<<endl; n--; } return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
my c++ solution taking care of special cases