Construct the Longest New String

You are given three integers x, y, and z.

You have x strings equal to "AA", y strings equal to "BB", and z strings equal to "AB". You want to choose some (possibly all or none) of these strings and concatenate them in some order to form a new string. This new string must not contain "AAA" or "BBB" as a substring.

Return the maximum possible length of the new string.

A substring is a contiguous non-empty sequence of characters within a string.

Example 1
Inputx = 2, y = 5, z = 1
Output12
Concatenating "BB", "AA", "BB", "AA", "BB", and "AB" forms "BBAABBAABBAB" with length 12, and it is impossible to construct a longer valid string.
Example 2
Inputx = 3, y = 2, z = 2
Output14
Concatenating "AB", "AB", "AA", "BB", "AA", "BB", and "AA" forms "ABABAABBAABBAA" with length 14, and it is impossible to construct a longer valid string.

Constraints

  • 1 <= x, y, z <= 50

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate