Problem title
Difficulty
Avg time to solve

Time To Burn Tree
Hard
50 mins
Partition a set into two subsets such that the difference of subset sums is minimum.
Hard
10 mins
Chocolate Pickup
Hard
--
Children Sum Property
Moderate
36 mins
Sort a Stack
Easy
10 mins
Print Permutations - String
Easy
30 mins
Next Greater Element
Moderate
20 mins
Binary Search
Easy
15 mins
Maximum Path Sum Between Two Leaves
Hard
50 mins
Valid Pairs
Easy
22 mins
47

Overlapping ABBA

Difficulty: EASY
Contributed By
Anish De |Level 1
Avg. time to solve
10 min
Success Rate
85%

Problem Statement

Anish is given a string S and has been asked to determine if the given string S contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).

As a friend of Anish, your task is to return “True” if the string S contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order) otherwise return “False” (without quotes).

Example:-
The string “ABBA” has two non-overlapping substrings “AB” and “BA” respectively. So “True” will be printed(without quotes).
Input format :
The first line contains a single integer T representing the number of test cases.

The first line of each test case contains the string S denoting the given string to Anish.
Output format :
For each test case, print "True"  if the string S contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order) else print "False” (without quotes).

The output of each test case should be printed in a separate line.
Note:
You are not required to print anything, it has already been taken care of. Just implement the function.    
Constraints :
1 <= T <= 10
1 <= |S| <= 10^4
The string S contains uppercase Latin letters only.
Time Limit = 1 sec
Sample Input 1 :
2
ABA
BACFAB
Sample Output 1 :
False
True
Explanation for Sample Output 1 :
In the first test case, there are no two non-overlapping substrings, so “False” is printed.

In the second test case, there are two non-overlapping substrings (BACFAB), so “True” is printed. 
Sample Input 2 :
2
ABBA
AXBYBXA
Sample Output 2 :
True
False
Reset Code
Full screen
copy-code
Console