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 itertools import combinations
def finding(test_str):
    ab = [];
    ba = [];
    i = test_str.find('AB');
    j = test_str.find('BA');

    while True:
        if(i != -1):
            ab.append(i);
            i = test_str.find('AB',i+1);
        elif j != -1:
            ba.append(j);
            j = test_str.find('BA',j+1)
        if(i == -1 and j == -1):
            break;
    for g in ab:
        for s in ba:
            if(g+1 != s and g!= s and g-1 != s):
                return 'YES';
    return 'NO';

str = input()
print(finding(str));

