forked from projectX-india/madlab-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path105A.cpp
97 lines (92 loc) · 2.36 KB
/
105A.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
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
88
89
90
91
92
93
94
95
96
97
#include<bits/stdc++.h>
#include <iostream.h>
using namespace std;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
typedef long long ll;
typedef long double ld;
#define pii pair<ll,ll>
#define vi vector< ll >
#define vvi vector< vi >
#define vpi vector< pii >
#define vvpi vector< vpi >
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define test(t) ll t;cin>>t;while(t--)
#define mem(a,b) memset(a,b,sizeof(a))
#define inn freopen("input.txt", "r", stdin)
#define outt freopen("output.txt", "w", stdout)
#define all(arr) arr.begin(),arr.end()
#define fr(i,n) for(ll i=0;i<(n);++i)
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define per(i,a,b) for(ll i=a;i>=b;i--)
#define remin(a,b) (a=min((a),(b)))
#define remax(a,b) (a=max((a),(b)))
/*=============================================================================
# Author: Parv Gupta - https://github.com/parvg555/
# Email: parvg555@gmail.com
# FileName: 105A.cpp
# Created On: 02/03/2021
# Problem Code: abc string
=============================================================================*/
bool checkcker(char s[], ll len){
// checker functon
if(s[0]==s[len-1]){
return false;
}
stack <char> checker;
for(int i=0;i<len;i++){
if(s[i]=='('){
checker.push('(');
}else{
if(checker.size()!=0){
checker.pop();
}else{
return false;
}
}
}
// for(int i=0;i<n;i++)
// return i;
//if(res = 0)
//return;
if(checker.size()!=0){
return false;
}
return true;
}
void sol(){
string s;
cin>>s;
ll len = s.length();
char ans1[len];
char ans2[len];
char first = s[0];
char end = s[len-1];
for(int i=0;i<len;i++){
if(s[i]==first){
ans1[i]='(';
ans2[i]='(';
}else if(s[i]==end){
ans1[i]=')';
ans2[i]=')';
}else{
ans1[i]=')';
ans2[i]='(';
}
}
//checking with (
if(checkcker(ans1,len)==true || checkcker(ans2,len)==true){
cout<<"YES\n";
return;
}
cout<<"NO\n";
}
int main(void){
fast;
test(t){
sol();
}
}