Problem Description
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. The new string must not contain "AAA" or "BBB" as a substring. Return the maximum possible length of such a new string.
Key Insights
- We have pieces of fixed length (always 2 characters) that can be concatenated in any order.
- The main constraint is to avoid any substring "AAA" or "BBB" which means we cannot allow three consecutive A’s or B’s.
- When appending a piece, note that the characters at the junction may merge with the tail of the current string; therefore, we must carefully simulate the merging.
- A dynamic programming (or DFS with memoization) solution can be designed where the state consists of the remaining counts (x, y, z) and the last up to two characters (i.e. a suffix) of the current string.
- For each state, try to append any piece that does not violate the constraint by checking if the new suffix contains "AAA" or "BBB". Then update the state accordingly.
- The total length increases by 2 for every piece appended, and our goal is to maximize the total appended length.
Space and Time Complexity
Time Complexity: O(x * y * z * C) where C is the number of possible suffix states (bounded by a small constant, at most 4 characters states)
Space Complexity: O(x * y * z * C) due to recursion stack and memoization storage.
Solution
We use a recursive depth-first search (DFS) with memoization. The state is defined by four parameters:
- The remaining counts of "AA", "BB", and "AB" pieces (x, y, z).
- The current suffix (last up to 2 characters) of the concatenated string.
For every state, we try appending one of the three available pieces (if available). Before appending, we concatenate the current suffix with the chosen piece and check if the resulting string (or its relevant suffix) contains "AAA" or "BBB". If appending does not violate the rule, we update the suffix to be the last two characters of the new string and continue recursively with decreased piece count.
The solution uses simple string operations to simulate the merging of pieces and verifies the constraints. We then choose the maximum length over all valid combinations.