Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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 <= 50Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming · Greedy
2 5 1
3 2 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2745: Construct the Longest New String
class Solution {
public int longestString(int x, int y, int z) {
if (x < y) {
return (x * 2 + z + 1) * 2;
}
if (x > y) {
return (y * 2 + z + 1) * 2;
}
return (x + y + z) * 2;
}
}
// Accepted solution for LeetCode #2745: Construct the Longest New String
func longestString(x int, y int, z int) int {
if x < y {
return (x*2 + z + 1) * 2
}
if x > y {
return (y*2 + z + 1) * 2
}
return (x + y + z) * 2
}
# Accepted solution for LeetCode #2745: Construct the Longest New String
class Solution:
def longestString(self, x: int, y: int, z: int) -> int:
if x < y:
return (x * 2 + z + 1) * 2
if x > y:
return (y * 2 + z + 1) * 2
return (x + y + z) * 2
// Accepted solution for LeetCode #2745: Construct the Longest New String
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2745: Construct the Longest New String
// class Solution {
// public int longestString(int x, int y, int z) {
// if (x < y) {
// return (x * 2 + z + 1) * 2;
// }
// if (x > y) {
// return (y * 2 + z + 1) * 2;
// }
// return (x + y + z) * 2;
// }
// }
// Accepted solution for LeetCode #2745: Construct the Longest New String
function longestString(x: number, y: number, z: number): number {
if (x < y) {
return (x * 2 + z + 1) * 2;
}
if (x > y) {
return (y * 2 + z + 1) * 2;
}
return (x + y + z) * 2;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.