Class Solution
-
- All Implemented Interfaces:
public final class Solution2182 - Construct String With Repeat Limit\.
Medium
You are given a string
sand an integerrepeatLimit. Construct a new stringrepeatLimitedStringusing the characters ofssuch that no letter appears more thanrepeatLimittimes in a row. You do not have to use all characters froms.Return the lexicographically largest
repeatLimitedStringpossible.A string
ais lexicographically larger than a stringbif in the first position whereaandbdiffer, stringahas a letter that appears later in the alphabet than the corresponding letter inb. If the firstmin(a.length, b.length)characters do not differ, then the longer string is the lexicographically larger one.Example 1:
Input: s = "cczazcc", repeatLimit = 3
Output: "zzcccac"
Explanation: We use all of the characters from s to construct the repeatLimitedString "zzcccac".
The letter 'a' appears at most 1 time in a row.
The letter 'c' appears at most 3 times in a row.
The letter 'z' appears at most 2 times in a row.
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
The string is the lexicographically largest repeatLimitedString possible so we return "zzcccac".
Note that the string "zzcccca" is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString.
Example 2:
Input: s = "aababab", repeatLimit = 2
Output: "bbabaa"
Explanation: We use only some of the characters from s to construct the repeatLimitedString "bbabaa".
The letter 'a' appears at most 2 times in a row. The letter 'b' appears at most 2 times in a row.
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
The string is the lexicographically largest repeatLimitedString possible so we return "bbabaa".
Note that the string "bbabaaa" is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString.
Constraints:
<code>1 <= repeatLimit <= s.length <= 10<sup>5</sup></code>
sconsists of lowercase English letters.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final StringrepeatLimitedString(String s, Integer repeatLimit)-
-
Method Detail
-
repeatLimitedString
final String repeatLimitedString(String s, Integer repeatLimit)
-
-
-
-