Skip to content

Practice Construct the Longest New String

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2745
LeetCode Problem

Construct the Longest New String

Problem Statement

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.

Source: Construct the Longest New String on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
x = 2, y = 5, z = 1
Output
12

# Example 2

Input
x = 3, y = 2, z = 2
Output
14

Constraints

1 <= x, y, z <= 50

Related LeetCode Problems

Practice more problems similar to Construct the Longest New String.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.