using namespace std;
int main()
{
string s;
long long int a=0,b=0,t=0;
cin>>s;
if(s.length()<4)
{
cout<<"NO";
return 0;
}
else
{
for(int i=0;i<s.length();i++)
{
if(s[i]=='A'&&s[i+1]=='B'&&a==0)
{
a=1;
t=i+1;
break;
}
}
for(int i=0;i<s.length();i++)
{
if(s[i]=='B'&&s[i+1]=='A'&&a==1&&i!=t||i!=t+1)
{
b=1;
break;
}
}
if(a==1&&b==1)
{
cout<<"YES";
return 0;
}
else
{
cout<<"NO";
return 0;
}
}
}```
I am getting the wrong answer in test case 8 and IDK why pls help!
I think your code might fail on this -
ABAAB
Your code ignores the second AB and takes into consideration of the first one. Because of this, non-overlapping BA could not be found.
2 Likes