-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balanced Brackets_hr.cpp
40 lines (36 loc) · 1.02 KB
/
Balanced Brackets_hr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int main(){
fast;
#ifndef ONLINE_JUDGE
freopen("inputf.in","r",stdin);
freopen("outputf.out","w",stdout);
#endif
//////////////////////////////////
int n;
string s;
cin>>n;
stack<char>st;
while(n--){
int flag=1;
st=stack<char>();
cin>>s;
for(int i=0;i<s.size();i++){
char b=s[i];
if(b=='(' || b=='[' || b=='{'){
st.push(b);
}else{
if(st.empty()){flag=0;break;}
char b2=st.top();
if(b2=='('&&b==')' || b2=='['&&b==']' || b2=='{'&&b=='}'){
st.pop();
}else {flag=0;break;}
}
}
if(flag==0)cout<<"NO\n";
else if(st.empty())cout<<"YES\n";
else cout<<"NO\n";
}
return 0;}