↓Skip to main content
  1. LeetCode/

LeetCode 2745: Construct the Longest New String

·2 mins· ·
LeetCode Medium Dynamic-Programming Math Greedy Brainteaser
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-22

  • Submissions: Accepted (first try)

Approach
#

AAA and BBB are forbidden:

  • AA cannot be followed by AA or AB (that makes AAA); only BB.
  • BB cannot be followed by BB; AA or AB are fine.
  • AB may be followed by AA or AB, not BB.

So AA and BB must alternate, AABBAABB..., using at most min(x, y) pairs; if one kind is more common, one more fits at an end. All ABs chain together between a BB and an AA (...BB ABAB AA...), so every one is used.

x == y: (2x + z) × 2
x != y: (2 × min(x, y) + 1 + z) × 2

My Solution
#

class Solution:
    def longestString(self, x: int, y: int, z: int) -> int:
        pairs = min(x, y)
        pieces = 2 * pairs + (1 if x != y else 0) + z     # strings used
        return pieces * 2                                 # each string has length 2

LeetCode tags this as DP (dp[x][y][z][last] works), but with the pattern found it is O(1).

Complexity
#

Time O(1), space O(1).

Mistakes to Avoid
#

  • Assuming AB is restricted: ABAB creates no AAA or BBB, so all of them fit.

Interview Explanation
#

AA can only be followed by BB, so AA and BB alternate, using twice the smaller count plus one more of the larger kind; all ABs chain together between a BB and an AA. Count the strings used and multiply by 2. Time O(1).

Related

LeetCode 313: Super Ugly Number
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 313 note: generalizing LC 264 with one pointer per prime; with a heap, avoid pushing the same number many times
LeetCode 396: Rotate Function
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 396 note: F(k) and F(k - 1) differ by one formula: F(k) = F(k - 1) + sum - n × the element moved to the front
LeetCode 397: Integer Replacement
·2 mins
LeetCode Medium Dynamic-Programming Greedy Bit-Manipulation Memoization
LeetCode 397 note: even numbers are always halved; for odd numbers try +1 and -1 with memoized recursion, since n is too large for an n-sized array
LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 264: Ugly Number II
·2 mins
LeetCode Medium Hash-Table Math Dynamic-Programming Heap-Priority-Queue
LeetCode 264 note: three pointers mark the next ugly numbers to multiply by 2, 3 and 5; take the minimum each time and advance every pointer that produced it
LeetCode 1025: Divisor Game
·2 mins
LeetCode Easy Math Dynamic-Programming Brainteaser Game-Theory
LeetCode 1025 note: dp[i] holds when some divisor x of i makes dp[i - x] a loss; observing the pattern, the answer is simply whether n is even