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
from collections import Counter, defaultdict
from functools import lru_cache
import sys
import threading

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

    @lru_cache(None)
    def dp(idx, ab_found, ba_found):
        if ab_found and ba_found:
            return True

        if idx >= n-1:
            return False
        
        cur = s[idx] + s[idx+1]
        if cur == "AB" and not ab_found:
            return dp(idx + 1, ab_found , ba_found) or dp(idx + 2, True, ba_found)

        elif cur == "BA" and not ba_found:
            return dp(idx + 1, ab_found,ba_found ) or dp(idx + 2, ab_found, True)

        else:
            return dp(idx + 1, ab_found, ba_found)

    if dp(0, False, False):
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    sys.setrecursionlimit(1 << 30)
    threading.stack_size(1 << 27)

    main_thread = threading.Thread(target=main)
    main_thread.start()
    main_thread.join()