2745. Construct the Longest New String LeetCode Solution
In this guide, you will get 2745. Construct the Longest New String LeetCode Solution with the best time and space complexity. The solution to Construct the Longest New String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Construct the Longest New String solution in C++
- Construct the Longest New String solution in Java
- Construct the Longest New String solution in Python
- Additional Resources

Problem Statement of 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:
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.
Constraints:
1 <= x, y, z <= 50
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
2745. Construct the Longest New String LeetCode Solution in C++
class Solution {
public:
int longestString(int x, int y, int z) {
//"AB" can always be easily appended within the string.
// Alternating "AA" and "BB" can be appended, creating a pattern like "AABB"
// If x == y, we repeat the pattern "AABBAABB...AABB".
// If x != y, the pattern becomes "AABBAABB...AABBAA" or "BBAABBAABB...AABB"
const int mn = min(x, y);
if (x == y)
return (mn * 2 + z) * 2;
return (mn * 2 + 1 + z) * 2;
}
};
/* code provided by PROGIEZ */
2745. Construct the Longest New String LeetCode Solution in Java
class Solution {
public int longestString(int x, int y, int z) {
final int mn = Math.min(x, y);
if (x == y)
return (mn * 2 + z) * 2;
return (mn * 2 + 1 + z) * 2;
}
}
// code provided by PROGIEZ
2745. Construct the Longest New String LeetCode Solution in Python
class Solution:
def longestString(self, x: int, y: int, z: int) -> int:
# 'AB' can always be easily appended within the string.
# Alternating 'AA' and 'BB' can be appended, creating a pattern like 'AABB'
# If x == y, we repeat the pattern 'AABBAABB...AABB'.
# If x != y, the pattern becomes 'AABBAABB...AABBAA' or 'BBAABBAABB...AABB'
mn = min(x, y)
if x == y:
return (mn * 2 + z) * 2
return (mn * 2 + 1 + z) * 2
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.