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
repeatLimittimes 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
Visualization
Time & Space Complexity
O(n) to count frequencies, O(k log k) for heap operations where k โค 26, total construction is O(n)
O(k) space for heap and frequency counting where k โค 26 (alphabet size)
Constraints
- 1 โค s.length โค 105
- s consists of lowercase English letters
- 1 โค repeatLimit โค 105
- The result should be lexicographically largest possible