Longest String Without AAA or BBB

Asked at Zalando · Coding Questions · Difficulty: Medium

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"` = `"AABBAABBAA"`. Note that it is not possible to add another `"AA"` string as the result would then contain `"AAA"`.

Loading the problem…