Ad Code

Responsive Advertisement

673 - Parentheses Balance

Author: Ismail Hosen

Daffodil International University





Problem Link



//Please follow from Main Function



#include<bits/stdc++.h>



using namespace std;



typedef long long ll;



int main()

{

    ll a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;

    a=b=c=d=e=f=0;

    cin>>n;

    cin.ignore();

    while(n--) {

       stack<char>st;

       string arr;

       getline(cin, arr);

       ll ln = arr.size();



       for(i=0; i<ln; i++){

          if(arr[i]=='(' || arr[i]=='[') st.push(arr[i]);

          else{

            if(arr[i]==')' && (st.empty() || st.top()!='(')){

                st.push('x');

                break;

            }



            if(arr[i]==']' && (st.empty() || st.top()!='[')){

                st.push('x');

                break;

            }

            st.pop();

          }

       }

       if(st.empty()) cout<<"Yes"<<endl;

       else cout<<"No"<<endl;

    }

    return 0;

}




Post a Comment

0 Comments