FastPrepFastPrep
Problem Brief

Longest String Without AAA or BBB

OA

There are two-letter strings, "AA", "AB" and "BB", which appear AA, AB and BB times respectively. The task is to join some of these strings to create the longest possible string which does not contain "AAA" or "BBB". For example, having AA = 5, AB = 0 and BB = 2, it is possible to join five strings by taking both of the "BB" strings and three of the "AA" strings. Then they can be joined into "AA-BB-AA-BB-AA" = "AABBAABBA". Note that it is not possible to add another "AA" string as the result would then contain "AAA".

Given three integers AA, AB and BB, returns the longest string that can be created according to the rules described above. If there is more than one possible answer, the function may return any of them.

1Example 1

Input
AA = 5, AB = 0, BB = 2
Output
"AABBAABBA"
Explanation
Given AA = 5, AB = 0 and BB = 2, the function should return "AABBAABBA", as explained above.

2Example 2

Input
AA = 1, AB = 2, BB = 1
Output
"BBAABAA"
Explanation
Given AA = 1, AB = 2 and BB = 1, possible results are "BBAABAA", "ABABAB", "ABAABAB" or "AABBAAB".

3Example 3

Input
AA = 0, AB = 2, BB = 0
Output
"ABAB"
Explanation
Given AA = 0, AB = 2 and BB = 0, the function should return "ABAB".

4Example 4

Input
AA = 0, AB = 0, BB = 10
Output
"BB"
Explanation
Given AA = 0, AB = 0 and BB = 10, the function should return "BB".

Constraints

Limits and guarantees your solution can rely on.

  • AA, AB and BB are integers within the range [0..10].
  • The resulting string will not be empty.
public String solution (int AA, int AB, int BB) {
  // write your code here
}
  
Input

AA

5

AB

0

BB

2

Output

"AABBAABBA"

Sign in to submit your solution.