You are given a string s and an integer repeat_limit. Construct the lexicographically largest string using the characters of s such that no letter appears more than repeat_limit times in a row.
You do not have to use all characters from s.
Return the lexicographically largest string you can construct.
Function Signature
def repeat_limited_string(s: str, repeat_limit: int) -> str:
Rules
-
Each character of
s
can be used at most once, and the characters can be placed in any order.
-
Strings are compared position by position, and the first differing letter decides. If one string is a prefix of the other, the longer string is larger.
Constraints
-
1 <= repeat_limit <= len(s) <= 10^5
-
s
consists of lowercase English letters.
Examples
Input: s = "cczazcc", repeat_limit = 3
Output: "zzcccac"
s has two z, four c and one a. Both z go first. Only three c may follow in a row, so the single a breaks the run and the fourth c comes last. Every character is used.
Input: s = "aababab", repeat_limit = 2
Output: "bbabaa"
s has four a and three b. Two b go first, one a breaks the run, and the last b follows. The three remaining a can only form a run of two at the end, so one a is left unused.
Input: s = "zzzza", repeat_limit = 1
Output: "zaz"
With a limit of 1, every z needs a different letter next to it. The single a can separate only two z, so the other two z are dropped.