BijonDurjoy

MY stack

Jan 12th, 2022 (edited)
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///BIJON SAHA DURJOY
  2. // SUST_SWE19
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. bool isvalid(string s)
  7. {
  8.      stack<char> stk;
  9.      bool ans = true;
  10.      long long int n,i;
  11.      n= s.size();
  12.      for(i=0; i<n; i++)
  13.      {
  14.         if(s[i] == '(' || s[i] == '[')
  15.         {
  16.              stk.push(s[i]);
  17.         }
  18.         else if(s[i] == ')')
  19.         {
  20.              if(!stk.empty() && stk.top() == '(')
  21.              {
  22.                   stk.pop();
  23.              }
  24.         }
  25.         else if(s[i] == ']')
  26.         {
  27.              if(!stk.empty() && stk.top() == '[')
  28.              {
  29.                   stk.pop();
  30.              }
  31.         }
  32.         else
  33.         {
  34.              ans = false;
  35.              break;
  36.         }
  37.      }
  38.      if(!stk.empty())
  39.      {
  40.           return false;
  41.      }
  42.      else
  43.      {
  44.           return true;
  45.      }
  46. }
  47. int main()
  48. {
  49.      long long int t,i;
  50.      cin>>t;
  51.      while(t--)
  52.      {
  53.           string s;
  54.           cin>>s;
  55.           if(isvalid(s))
  56.           {
  57.                cout<<"Yes"<<endl;
  58.           }
  59.           else
  60.           {
  61.                cout<<"No"<<endl;
  62.           }
  63.      }
  64.      return 0;    
  65. }
Advertisement
Add Comment
Please, Sign In to add comment