Construct String With Repeat Limit

You are given a string s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no letter appears more than repeatLimit times in a row. You do not have to use all characters from s.

Return the lexicographically largest repeatLimitedString possible.

A string a is lexicographically larger than a string b if in the first position where a and b differ, string a has a letter that appears later in the alphabet than the corresponding letter in b. If the first min(a.length, b.length) characters do not differ, then the longer string is the lexicographically larger one.

Example 1
Inputs = "cczazcc", repeatLimit = 3
Output"zzcccac"
Using all characters forms a valid string where no character appears more than 3 times in a row, and any lexicographically larger arrangement would violate the repeat limit.
Example 2
Inputs = "aababab", repeatLimit = 2
Output"bbabaa"
Using only some characters forms a valid string where no character appears more than 2 times in a row, and any lexicographically larger arrangement would violate the repeat limit.

Constraints

  • 1 <= repeatLimit <= s.length <= 10^5
  • s consists of lowercase English letters.

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate