Construct String With Repeat Limit - Problem

You're tasked with creating the lexicographically largest string possible from given characters, but with a twist: no character can appear more than repeatLimit times consecutively.

Given a string s and an integer repeatLimit, construct a new string using the characters from s such that:

  • No letter appears more than repeatLimit times in a row
  • The result is the lexicographically largest possible string
  • You don't need to use all characters from s

What does lexicographically larger mean? String a is lexicographically larger than string b if at the first differing position, a has a character that comes later in the alphabet than the corresponding character in b.

Example: For s = "cczazcc" and repeatLimit = 3, we want to maximize our string by using characters in descending order: 'z', 'c', 'c', 'c', 'a', 'c', 'c' โ†’ "zccaccc"

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "cczazcc", repeatLimit = 3
โ€บ Output: "zcccaca"
๐Ÿ’ก Note: We want the lexicographically largest string. Start with 'z' (largest), then add 'c' up to limit (3), then need separator 'a', then remaining 'c', then remaining 'a'.
example_2.py โ€” Single Character Type
$ Input: s = "aababab", repeatLimit = 2
โ€บ Output: "bbabaa"
๐Ÿ’ก Note: We have 'b':3 and 'a':4. Start with 'b', add 2 (limit), then 'a' as separator, then 'b', then remaining 'a's with separator pattern.
example_3.py โ€” No Repeat Limit Hit
$ Input: s = "aab", repeatLimit = 5
โ€บ Output: "baa"
๐Ÿ’ก Note: Since repeatLimit is 5 and we have at most 2 of any character, we can simply arrange in descending order: 'b' then 'a','a'.

Visualization

Tap to expand
VIP Restaurant Seating StrategyVIP Level ZCount: 1VIP Level CCount: 4VIP Level ACount: 2Seating Process (repeatLimit = 3)ZSeat: ZCCCSeat: CCC (limit)ASeparatorCResume CARemainingFinal Seating: Z-C-C-C-A-C-Aโœจ Always prioritize highest VIP level available๐ŸŽฏ Use lower VIP levels strategically as separatorsโšก Max heap ensures efficient VIP level management
Understanding the Visualization
1
Count VIP Types
Count how many guests of each VIP level we have
2
Priority Queue
Use a priority system to always serve the highest VIP level available
3
Seat with Limits
Seat up to 'repeatLimit' guests of the highest level
4
Strategic Separation
When hitting the limit, seat one guest of the next highest level as a separator
5
Repeat Process
Continue until all guests are seated or no valid arrangement exists
Key Takeaway
๐ŸŽฏ Key Insight: Use a max heap to always prioritize the largest available character, and strategically use smaller characters as separators when hitting the repeat limit. This greedy approach guarantees the lexicographically largest result.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + k log k)

O(n) to count frequencies, O(k log k) for heap operations where k โ‰ค 26, total construction is O(n)

n
2n
โšก Linearithmic
Space Complexity
O(k)

O(k) space for heap and frequency counting where k โ‰ค 26 (alphabet size)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of lowercase English letters
  • 1 โ‰ค repeatLimit โ‰ค 105
  • The result should be lexicographically largest possible
Asked in
Google 23 Amazon 18 Meta 12 Microsoft 8
28.4K Views
Medium-High Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen