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.
from bisect import bisect_left

def solve():
    s = input()
    n = len(s)

    ab_inds, ba_inds = [], []
    for i in range(n-1):
        if s[i:i+2] == 'AB':
            ab_inds.append(i)
        elif s[i:i+2] == 'BA':
            ba_inds.append(i)
    len_ab, len_ba = len(ab_inds), len(ba_inds)
    if not ab_inds or not ba_inds:
        print('NO')
        return

    for i in ab_inds:
        j = bisect_left(ba_inds, i+2)
        if j < len_ba: 
            print('YES')
            return 
    
    for i in ba_inds:
        j = bisect_left(ab_inds, i+2)
        if j < len_ab: 
            print('YES')
            return
    
    print('NO')
    return 

solve()