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 10^5 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.


-----Examples-----
Input
ABA

Output
NO

Input
BACFAB

Output
YES

Input
AXBYBXA

Output
NO



-----Note-----

In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO".

In the second sample test there are the following occurrences of the substrings: BACFAB.

In the third sample test there is no substring "AB" nor substring "BA".

Difficulty: interview

Starter code:

Solutions:
a = []
b = []
x = input()
for i in range(len(x)-1):
    if x[i]+x[i+1] == 'AB':
        a.append(i)
    elif x[i]+x[i+1] == 'BA':
        b.append(i)

if a == [] or b == []:
    print("NO")
    quit()
    
if abs(min(a)-max(b))>1 or abs(max(a)-min(b))>1:
    print("YES")
    quit()
print("NO")


S = input()
if S.count("AB") == 0 or S.count("BA") == 0 :
    print("NO")
else :
    if S[S.index("AB")+2:].count("BA") > 0 or S[S.index("BA")+2:].count("AB") > 0 :
            print("YES")
    else :
        print("NO")


def ok(s,a,b):
    indexa = s.find(a)
    if indexa == -1:
        return False
    return s.find(b,indexa+2) != -1
    
def __starting_point():
    s = input().strip()
    if ok(s , "AB","BA") or ok(s,"BA","AB"):
        print("YES")
    else:
        print("NO")

__starting_point()

import sys



input       = []
input_index = 0

def next(type, number = None):
	def next():
		nonlocal input, input_index
		
		
		while input_index == len(input):
			if sys.stdin:
				input       = sys.stdin.readline().split()
				input_index = 0
			else:
				raise Exception()
				
				
		input_index += 1
		
		return input[input_index - 1]
		
		
	if number is None:
		result = type(next())
	else:
		result = [type(next()) for _ in range(number)]
		
	return result
	
	
	
s = next(str)

_abs = []
_bas = []

for index in range(1, len(s)):
	if s[index - 1:index + 1] == "AB":
		_abs.append(index)
	elif s[index - 1:index + 1] == "BA":
		_bas.append(index)
		
if _abs and _bas and abs(min(_abs) - max(_bas)) >= 2:
	print("YES")
elif _abs and _bas and abs(max(_abs) - min(_bas)) >= 2:
	print("YES")
else:
	print("NO")


inputstr = input()
strlen = len(inputstr)

ab = []
ba = []

import re
for m in re.finditer('AB', inputstr):
	ab.append([ m.start(), m.end()-1 ])
for m in re.finditer('BA', inputstr):
	ba.append([ m.start(), m.end()-1 ])

found = False
for i in range(len(ab)):
	for j in range(len(ba)):
		if ab[i][0] != ba[j][0] and ab[i][0] != ba[j][1] and ab[i][1] != ba[j][0] and ab[i][1] != ba[j][1]:
			found = True
			break
	if found:
		break

if found:
	print("YES")
else:
	print("NO")

s=input()
ab=0
ba=0
aba=0
bab=0
n=len(s)
if n<4:
	print("NO")
else:
	for i in range(n-1):
		if s[i:i+2]=="AB":ab+=1
	for i in range(n-1):
		if s[i:i+2]=="BA":ba+=1
	for i in range(n-2):
		if s[i:i+3]=="ABA":aba+=1
	for i in range(n-2):
		if s[i:i+3]=="BAB":bab+=1
	if ab==0 or ba==0:
		print("NO")
	elif ab==1 and ba==1:
		if bab==1 or aba==1:
			print("NO")
		else:
			print("YES")
	elif ab==1 and ba==2:
		if bab==1 and aba==1:print("NO")
		else:print("YES")
	elif ab==2 and ba==1:
		if bab==1 and aba==1:print("NO")
		else:print("YES")
	else:print("YES")

def main():
    import sys
    
    s = sys.stdin.read().strip()
    n = len(s)
    
    result = False
    flag = False
    i = 0
    while i < n - 1:
        if not flag and s[i: i + 2] == "AB":
            flag = True
            i += 2
        elif flag and s[i: i + 2] == "BA":
            result = True
            break
        else:   
            i += 1
    flag = False
    i = 0
    while i < n - 1:
        if not flag and s[i: i + 2] == "BA":
            flag = True
            i += 2
        elif flag and s[i: i + 2] == "AB":
            result = True
            break
        else:   
            i += 1

    print("YES" if result else "NO")

main()


def __starting_point():
    s = input()

    a = None
    b = None

    for i in range(len(s) - 1):
        if s[i] == 'A' and s[i + 1] == 'B':
            if a is None:
                a = i

            if b is not None and abs(i - b) > 1:
                print('YES')
                return

        if s[i] == 'B' and s[i + 1] == 'A':
            if b is None:
                b = i

            if a is not None and abs(a - i) > 1:
                print('YES')
                return


    print('NO')


__starting_point()

def ham(a,b):
    if a[1]==b[0] or b[1]==a[0]:
        return 0
    return 1
a = input()
ab = []
ba = []
for i in range(len(a)-1):
    if a[i:i+2]=="AB":
        ab.append([i,i+1])
    if a[i:i+2]=="BA":
        ba.append([i,i+1])
s = 0
for i in range(len(ab)):
    for l in range(len(ba)):
        if ham(ab[i],ba[l]):
            print("YES")
            s = 1
            break
    if s==1:
        break
if s==0:
    print("NO")
    


s = input()

abfound = bafound = -1
for i in range(len(s) - 1):
    if abfound == -1 and s[i] == 'A' and s[i + 1] == 'B':
        abfound = i
    elif bafound == -1 and s[i] == 'B' and s[i + 1] == 'A':
        bafound = i
    if abfound != -1 and bafound != -1:
        break

if abfound == -1 or bafound == -1:
    print("NO")
    return

if abfound == bafound + 1 or bafound == abfound + 1:
    for i in range(max(bafound + 2, abfound + 2), len(s) - 1):
        if s[i] == 'A' and s[i + 1] == 'B':
            newabfound = True
            print("YES")
            return
        elif s[i] == 'B' and s[i + 1] == 'A':
            newbafound = True
            print("YES")
            return
else:
    print("YES")
    return

print("NO")


s = input()
s1 = s.find("AB")
while s1 != -1:
    if (s[:s1].find("BA") != -1) or (s[s1+2:].find("BA") != -1):
        print("YES")
        return
    s1 = s.find("AB", s1+1)
print("NO")


s = input()

yes = False
try:
    ab = s.index('AB')
    ba = s.rindex('BA')
    if ba-ab > 1:
        yes = True

    ab = s.index('BA')
    ba = s.rindex('AB')
    if ba-ab > 1:
        yes = True
except ValueError:
    pass

print('YES' if yes else 'NO')

inp = input().strip()

if len(inp) < 4:
	print("NO")
else:
	if inp.count("AB") == 1 and inp.count("BA") == 1:
		if inp.count("ABA") + inp.count("BAB") >= 1:
			print("NO")
		else:
			print("YES")
	elif inp.count("AB") < 1 or inp.count("BA") < 1:
		print("NO")
	elif inp.count("AB") + inp.count("BA") == 3 and inp.count("ABAB") + inp.count("BABA") >= 1:
		print("NO")
	else:
		print("YES")

import copy
si = list(input())
s = copy.deepcopy(si)

c1 = 0
i = 0
while i < (len(s)-1):
    if s[i:i+2] == ['A', 'B']:
        s[i] = '.'
        s[i+1] = '.'
        c1 = 1
        break
    i += 1
c2 = 0
i = 0
while i < (len(s)-1):
    if s[i:i+2] == ['B', 'A']:
        s[i] = '.'
        s[i+1] = '.'
        c2 = 1
        break
    i += 1

if c1 and c2:
    c3 = 1
else:
    c3 = 0

s = copy.deepcopy(si)
c2 = 0
i = 0
while i < (len(s)-1):
    if s[i:i+2] == ['B', 'A']:
        s[i] = '.'
        s[i+1] = '.'
        c2 = 1
        break
    i += 1
c1 = 0
i = 0
while i < (len(s)-1):
    if s[i:i+2] == ['A', 'B']:
        s[i] = '.'
        s[i+1] = '.'
        c1 = 1
        break
    i += 1

if (c1 and c2) or c3:
    print('YES')
else:
    print('NO')

s = input()
pos = -1
pos1 = -1
for i in range(len(s) - 1):
    if s[i] == 'A' and s[i + 1] == 'B':
        pos = i
        break
for i in range(len(s) - 1, 0, -1):
    if s[i] == 'A' and s[i - 1] == 'B':
        pos1 = i - 1
        break
pos2 = -1
pos3 = -1
for i in range(len(s) - 1):
    if s[i] == 'B' and s[i + 1] == 'A':
        pos2 = i
        break
for i in range(len(s) - 1, 0, -1):
    if s[i] == 'B' and s[i - 1] == 'A':
        pos3 = i - 1
        break
if pos == -1 or pos1 == -1:
    flag = False
else:
    if abs(pos - pos1) > 1:
        flag = True
    else:
        flag = False
if flag:
    print('YES')
else:
    if pos2 == -1 or pos3 == -1:
        print('NO')
    else:
        if abs(pos2 - pos3) > 1:
            print('YES')
        else:
            print('NO')   

s = input()

i1 = s.find('AB')
i2 = s.find('BA')
found = False
if i1 >= 0:
    i3 = s.find('BA', i1+2)
    if i3 != -1:
        print("YES")
        found = True
if not found and i2 >= 0:
    i3 = s.find('AB', i2 + 2)
    if i3 != -1:
        print("YES")
        found = True
if not found:
    print("NO")

def is_ab(prev, current):
    return prev == 'A' and current == 'B'


def is_ba(prev, current):
    return prev == 'B' and current == 'A'


def is_matched(prev, current):
    return is_ab(prev, current) or is_ba(prev, current)


def main():
    s = input()
    i = 0
    total_ab = 0
    total_ba = 0
    total_confused = 0

    current_len = 0

    prev = ''

    while i < len(s):
        current = s[i]
        if current_len != 0:
            if is_matched(prev, current):
                current_len += 1
            else:
                if current_len == 2:
                    if is_ab(s[i - 2], s[i - 1]):
                        total_ab += 1
                    elif is_ba(s[i - 2], s[i - 1]):
                        total_ba += 1
                elif current_len == 3:
                    total_confused += 1
                elif current_len == 4:
                    total_confused += 1
                elif current_len > 4:
                    total_confused += 2
                if current == 'A' or current == 'B':
                    current_len = 1
                else:
                    current_len = 0
        elif current == 'A' or current == 'B':
            current_len = 1

        prev = current
        i += 1

    if len(s) >= 2 and current_len >= 2:
        current = s[len(s) - 1]
        prev = s[len(s) - 2]
        if current_len == 2:
            if is_ab(prev, current):
                total_ab += 1
            elif is_ba(prev, current):
                total_ba += 1
        elif current_len == 3:
            total_confused += 1
        elif current_len == 4:
            total_confused += 1
        elif current_len > 4:
            total_confused += 2

    # print(total_ba, total_confused, total_ab)
    if total_ba >= 1 and total_ab >= 1:
        print('YES')
    elif total_confused >= 2:
        print('YES')
    elif (total_ab >= 1 or total_ba >= 1) and total_confused >= 1:
        print('YES')
    else:
        print('NO')


def __starting_point():
    main()
__starting_point()

#!/usr/bin/env python3
import re
S = input()

if S.find("AB") >= 0 and "BA" in S[S.find("AB") + 2:]:
    print("YES")
elif S.find("BA") >= 0 and "AB" in S[S.find("BA") + 2:]:
    print("YES")
else:
    print("NO")

s = input()
lenS = len(s)
i = 0
t = s[:]
if 'AB' in t:
    i = t.index('AB')
    t = t[:i] + 'CC' + t[i+2:]
    if 'BA' in t:
        print('YES')
        return
t = s[:]
if 'BA' in t:
    i = t.index('BA')
    t = t[:i] + 'CC' + t[i+2:]
    if 'AB' in t:
        print('YES')
        return
print('NO')


s = input()
i = 0
ab = False
ba = False
o = 0
while i < len(s) - 1:
    if i < len(s) - 2 and (not o and not ab and not ba) and (s[i:i + 2] == 'AB' and s[i + 1:i + 3] == 'BA') or (s[i:i + 2] == 'BA' and (s[i + 1:i + 3] == 'AB')):
        o += 1
        i += 2
    elif s[i:i + 2] == 'AB' and not ab:
        ab = True
        i += 1
    elif s[i:i + 2] == 'BA' and not ba:
        ba = True
        i += 1
    i += 1
if ab + ba + o >= 2:
    print('YES')
else:
    print('NO')

s = input()
if s.find('AB') != -1 and s[s.find('AB') + 2:].find('BA') != -1 or \
   s.find('BA') != -1 and s[s.find('BA') + 2:].find('AB') != -1:
    print('YES')
else:
    print('NO')

import sys
s = sys.stdin.readline()
i = s.find('AB')
j = s.find('BA')
if i != -1 and s.find('BA', i + 2) != -1:
    sys.stdout.write('YES\n')
elif j != -1 and s.find('AB', j + 2) != -1:
    sys.stdout.write('YES\n')
else:
    sys.stdout.write('NO\n')


import sys
s = sys.stdin.readline()
i = s.find('AB')
j = s.find('BA')
if i != -1 and s.find('BA', i + 2) != -1:
    sys.stdout.write('YES\n')
elif j != -1 and s.find('AB', j + 2) != -1:
    sys.stdout.write('YES\n')
else:
    sys.stdout.write('NO\n')


s = input()
i = s.find('AB')
j = s.find('BA')
if i != -1 and s.find('BA', i + 2) != -1:
    print('YES')
elif j != -1 and s.find('AB', j + 2) != -1:
    print('YES')
else:
   print('NO')


s = input()
i = s.find('AB')
j = s.find('BA')
if i != -1 and s.find('BA', i + 2) != -1:
    print('YES')
elif j != -1 and s.find('AB', j + 2) != -1:
    print('YES')
else:
   print('NO')