Imagine you're working with a string construction system where you have three types of building blocks:
- x blocks of type "AA"
- y blocks of type "BB"
- z blocks of type "AB"
Your goal is to concatenate some or all of these blocks in any order to create the longest possible string with one critical constraint: the final string must not contain "AAA" or "BBB" as a substring.
For example, if you have x=2, y=1, z=1, you have blocks ["AA", "AA", "BB", "AB"]. You could arrange them as "AA" + "BB" + "AA" + "AB" = "AABBAAB" (length 7), but you need to ensure no three consecutive A's or B's appear.
Return the maximum possible length of such a valid string.
Input & Output
Visualization
Time & Space Complexity
Just a few mathematical calculations regardless of input size
Only uses a constant amount of extra variables
Constraints
- 0 โค x, y, z โค 50
- At least one of x, y, z is positive
- Total blocks will not exceed 150