Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///BIJON SAHA DURJOY
- // SUST_SWE19
- #include<bits/stdc++.h>
- using namespace std;
- bool isvalid(string s)
- {
- stack<char> stk;
- bool ans = true;
- long long int n,i;
- n= s.size();
- for(i=0; i<n; i++)
- {
- if(s[i] == '(' || s[i] == '[')
- {
- stk.push(s[i]);
- }
- else if(s[i] == ')')
- {
- if(!stk.empty() && stk.top() == '(')
- {
- stk.pop();
- }
- }
- else if(s[i] == ']')
- {
- if(!stk.empty() && stk.top() == '[')
- {
- stk.pop();
- }
- }
- else
- {
- ans = false;
- break;
- }
- }
- if(!stk.empty())
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- int main()
- {
- long long int t,i;
- cin>>t;
- while(t--)
- {
- string s;
- cin>>s;
- if(isvalid(s))
- {
- cout<<"Yes"<<endl;
- }
- else
- {
- cout<<"No"<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment