You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
Input
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Output
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
string str;
while(cin>>str){
int l = str.size();
if(l <= 3) cout<<"NO"<<endl;
else{
int a = str.find("AB");
int b = str.find("BA", a + 2);
int c = str.find("BA");
int d = str.find("AB", c + 2);
if ((a != string::npos && b != string::npos) || (c != string::npos && d != string::npos))
puts("YES");
else
puts("NO");
}
}
return 0;
}