Advertisement
sajid161

11:1

Jan 8th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. bool check(char a,char b)
  4. {
  5.     return (a=='('&&b==')' || a=='{'&&b=='}' || a=='['&&b==']');
  6. }
  7. int main()
  8. {
  9.     int t;
  10.     cin>>t;
  11.     while(t--)
  12.     {
  13.         string s;
  14.         cin>>s;
  15.         bool done=1;
  16.         stack<char>st;
  17.         for(auto u:s)
  18.         {
  19.             if(u=='(' ||u=='{' ||u=='[') st.push(u);
  20.             else
  21.             {
  22.                 if(st.empty())
  23.                 {
  24.                    done=0;
  25.                    break;
  26.                 }
  27.                 else
  28.                 {
  29.                     if(check(st.top(),u))
  30.                     {
  31.                         st.pop();
  32.                     }
  33.                     else
  34.                     {
  35.                         done=0;
  36.                         break;
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.         if(!st.empty()) done=0;
  42.         if(done==1) cout<<"YES\n";
  43.         else cout<<"NO\n";
  44.  
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement