#2745

Construct the Longest New String

specialist · 805 · lc medium +31 · verified · 54.6% accepted · 339 likes · top 47%

Description

You have x copies of "AA", y copies of "BB", and z copies of "AB". Concatenate a selection of them in any order such that the result never contains "AAA" or "BBB" as a substring. Return the maximum possible length.

Example 1:

Input: x = 2, y = 5, z = 1
Output: 12
Explanation: We can concatenate the strings "BB", "AA", "BB", "AA", "BB", and "AB" in that order. Then, our new string is "BBAABBAABBAB".
That string has length 12, and we can show that it is impossible to construct a string of longer length.

Example 2:

Input: x = 3, y = 2, z = 2
Output: 14
Explanation: We can concatenate the strings "AB", "AB", "AA", "BB", "AA", "BB", and "AA" in that order. Then, our new string is "ABABAABBAABBAA".
That string has length 14, and we can show that it is impossible to construct a string of longer length.

Code

1
2
3