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 math import ceil
from collections import defaultdict, deque

s = input()
if "AB" not in s or "BA" not in s:
    print("NO")
else:
    # find leftmost AB
    ab = s.index("AB")
    # find rightmost BA
    for i in reversed(range(len(s) - 1)):
        if (s[i], s[i + 1]) == ("B", "A"):
            ba = i
            break
    if ba > ab + 1:
        print("YES")
    else:
        # find rightmost AB
        for i in reversed(range(len(s) - 1)):
            if (s[i], s[i + 1]) == ("A", "B"):
                ba = i
                break
        # find leftmost BA
        ba = s.index("BA")
        if ab > ba + 1:
            print("YES")
        else:
            print("NO")