-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_ABC_String.cpp
More file actions
87 lines (70 loc) · 1.56 KB
/
A_ABC_String.cpp
File metadata and controls
87 lines (70 loc) · 1.56 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;
typedef long long ll;
void solve() {
string s;
cin>>s;
int n=s.size();
if(s[0]==s[n-1]){
cout<<"NO"<<endl;
return;
}
char fr=s[0],ls=s[n-1];
for(int i=0;i<n;i++){
if(s[i]==fr) s[i]='(';
else if(s[i]==ls) s[i]=')';
}
//if char le5er =);
string t=s;
for(int i=0;i<n;i++){
if(t[i]!='(' && t[i]!=')')t[i]='(';
}
stack<char> st;
bool ok=1;
for(int i=0;i<n;i++){
if(t[i]=='(')st.push(i);
else{
if(!st.empty())st.pop();
else {
ok=false;
break;
}
}
}
if(ok && st.empty()){
cout<<"YES"<<endl;
return;
}
stack<char> st2;
ok=1;
t=s;
for(int i=0;i<n;i++){
if(t[i]!='(' && t[i]!=')')t[i]=')';
}
for(int i=0;i<n;i++){
if(t[i]=='(')st2.push(i);
else{
if(!st2.empty())st2.pop();
else {
ok=false;
break;
}
}
}
if(ok && st2.empty()){
cout<<"YES"<<endl;
return;
}
cout<<"NO"<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}