Construct the Longest New String - Problem

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

example_1.py โ€” Basic Case
$ Input: x = 2, y = 5, z = 1
โ€บ Output: 16
๐Ÿ’ก Note: We have 2 "AA", 5 "BB", and 1 "AB" blocks. Since both x > 0 and y > 0, we can use all blocks. Total length = (2 + 5 + 1) ร— 2 = 16. One valid arrangement: "AA" + "BB" + "AA" + "AB" + "BB" + "BB" + "BB" + "BB".
example_2.py โ€” No AA Blocks
$ Input: x = 0, y = 3, z = 1
โ€บ Output: 4
๐Ÿ’ก Note: We have 0 "AA", 3 "BB", and 1 "AB" blocks. Since x = 0, we can use at most 1 "BB" block. Result = min(1, 3) ร— 2 + 1 ร— 2 = 2 + 2 = 4. Valid arrangement: "BB" + "AB".
example_3.py โ€” No BB Blocks
$ Input: x = 3, y = 0, z = 1
โ€บ Output: 4
๐Ÿ’ก Note: We have 3 "AA", 0 "BB", and 1 "AB" blocks. Since y = 0, we can use at most 1 "AA" block. Result = min(1, 3) ร— 2 + 1 ร— 2 = 2 + 2 = 4. Valid arrangement: "AA" + "AB".

Visualization

Tap to expand
String Construction StrategyAvailable Blocks:AAx blocksBBy blocksABz blocksStrategy: Use AB blocks as separatorsโŒ Bad:โ†’ "AAAA" (forbidden!)โœ… Good:โ†’ "AAABAA" (valid!)Mathematical SolutionCase Analysis:If x = 0: Only BB and AB blocks โ†’ max length = min(1, y) ร— 2 + z ร— 2If y = 0: Only AA and AB blocks โ†’ max length = min(1, x) ร— 2 + z ร— 2If x > 0 and y > 0: Can alternate/separate โ†’ max length = (x + y + z) ร— 2Time Complexity: O(1) | Space Complexity: O(1)
Understanding the Visualization
1
Identify Block Types
AA blocks (red), BB blocks (blue), AB blocks (mixed) each contribute 2 characters
2
Recognize Constraints
Cannot have AAA or BBB sequences in the final string
3
Use AB as Separators
AB blocks can break up potentially problematic AA-AA or BB-BB sequences
4
Apply Mathematical Formula
Calculate directly based on whether both AA and BB blocks are available
Key Takeaway
๐ŸŽฏ Key Insight: AB blocks are the most valuable because they act as separators, preventing forbidden AAA or BBB patterns while contributing 2 characters each. Mathematical analysis eliminates the need for expensive permutation testing.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Just a few mathematical calculations regardless of input size

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only uses a constant amount of extra variables

n
2n
โœ“ Linear Space

Constraints

  • 0 โ‰ค x, y, z โ‰ค 50
  • At least one of x, y, z is positive
  • Total blocks will not exceed 150
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen