 # ABC stringss

I need help with this code as i am getting WA don’t know why .
Any help would be appreciable .

link to the problem - Problem - 1494A - Codeforcesproblem
here’s a link to my code.
https://pastebin.com/a2vi2Rt8

That line is correct only because AABBCCAA should be (())((((. You should replace ‘A’ with the same type of bracket. For a regular bracket sequence the first char shud be ‘(’ and last char shud be ‘)’ which is not possible if we have same chars at first and last position.

1 Like

You should consider the first character of string as ‘(’ and last character as ‘)’. So both, the first and last char, need to be different. Now, the third alphabet(other than first and last) have two choices, either to be ‘(’ or ‘)’. Check if any of case is satisfied, the answer is “Yes”, else “no”.

void solve()
{
string s;
cin>>s;
//int s = 1, s[s.length()-1] = -1;
int ans1= 0, ans2 = 0;
if(s == s.back()){
cout<<“No”;
return;
}
int first=0,second=0;
for (int i = 0; i < s.length(); ++i)
{
if(s[i] == s)
first++;
else if( s[i] == s.back())
second++;
else ans1++;
}
if(ans1+first!=second and ans1+second!=first){
cout<<“No”;return;
}
int f=0;
if(first+ans1==second){
for(int i=0;i<s.length();i++){
if(s[i] == s)
ans2++;
else if( s[i] == s.back())
ans2–;
else ans2++;
if(ans2<0) f=1;
}
}else{
for(int i=0;i<s.length();i++){
if(s[i] == s)
ans2–;
else if( s[i] == s.back())
ans2++;
else ans2++;
if(ans2>0) f=1;
}
}
if(!f)
cout<<“Yes”;
else
cout<<“No”;}

1 Like

Yeah you are right @shashankpilla . I misread the questions. Thanks for point out the mistake!

I did the same thing i am taking two possibilities either the third element will be ‘(’ or ‘)’ and i am computing for both these cases subtracting 1 and adding 1 to the result .
if either of them results zero, the ans will be yes.
I want to figure out the mistake with my logic .
THANKS

You are not checking the order they are coming, like ans1 cannot be negative so that the order is maintained. Eg- ACCBBC , your code will give “yes”, but answer will be “no”.

1 Like

thanks for the help . 