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).
The only line of input contains a string *s* of length between 1 and 10<sup class="upper-index">5</sup> consisting of uppercase Latin letters.
#include<bits/stdc++.h>

using namespace std;
void mxx(int *a,int &n,vector<int>&temp,int idx){
	int r=idx-1;
for(int l=0;l<idx;l++){
		vector<int>te;
	for(int j=r+1;j<n;j++){
		
		te.push_back(a[j]);
	}
	for(int j=r;j>=l;j--){
	te.push_back(a[j]);	
	}
	for(int j=0;j<l;j++){
te.push_back(a[j]);
	}
	temp=max(temp,te);
}
int l=idx;
r=idx;

		vector<int>te;
	for(int j=r+1;j<n;j++){
		
		te.push_back(a[j]);
	}
	for(int j=r;j>=l;j--){
	te.push_back(a[j]);	
	}
	for(int j=0;j<l;j++){
te.push_back(a[j]);
	}
	temp=max(temp,te);
}
bool in_range(int i,int j,int n,int m){
	if(i>=1&&i<=n&&j>=1&&j<=m)return true;
	return false;
}
void Nozarashi(){
string s;
cin>>s;
vector<int>id1;
vector<int>id2;
for(int i=0;i<int(s.size())-1;i++){
	string q="";
	q+=s[i];
	q+=s[i+1];
	if(q=="AB")id1.push_back(i);
	else if(q=="BA")id2.push_back(i);
}
sort(id1.begin(),id1.end());
sort(id2.begin(),id2.end());
for(int i=0;i<int(id1.size());i++){
	long long id=id1[i]+2;
	long long idx=lower_bound(id2.begin(),id2.end(),id)-id2.begin();
	if(idx!=int(id2.size())){
		cout<<"YES"<<endl;
		return;
	}
long long idd=id1[i]-2;
long long l=0,r=id2.size()-1;
while(l<=r){
	long long mid=l+(r-l)/2;
	if(id2[mid]<=idd){
		cout<<"YES"<<endl;
		return;
	}
	else r=mid-1;
}
}
cout<<"NO"<<endl;
}
int main(){
	  std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

int t=1;
while(t--){
	
Nozarashi();
}

	return 0;
}

