|
| 1 | +<h2><a href="https://leetcode.com/problems/construct-string-with-repeat-limit/">2182. Construct String With Repeat Limit</a></h2><h3>Medium</h3><hr><div><p>You are given a string <code>s</code> and an integer <code>repeatLimit</code>. Construct a new string <code>repeatLimitedString</code> using the characters of <code>s</code> such that no letter appears <strong>more than</strong> <code>repeatLimit</code> times <strong>in a row</strong>. You do <strong>not</strong> have to use all characters from <code>s</code>.</p> |
| 2 | + |
| 3 | +<p>Return <em>the <strong>lexicographically largest</strong> </em><code>repeatLimitedString</code> <em>possible</em>.</p> |
| 4 | + |
| 5 | +<p>A string <code>a</code> is <strong>lexicographically larger</strong> than a string <code>b</code> if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears later in the alphabet than the corresponding letter in <code>b</code>. If the first <code>min(a.length, b.length)</code> characters do not differ, then the longer string is the lexicographically larger one.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong>Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre><strong>Input:</strong> s = "cczazcc", repeatLimit = 3 |
| 11 | +<strong>Output:</strong> "zzcccac" |
| 12 | +<strong>Explanation:</strong> We use all of the characters from s to construct the repeatLimitedString "zzcccac". |
| 13 | +The letter 'a' appears at most 1 time in a row. |
| 14 | +The letter 'c' appears at most 3 times in a row. |
| 15 | +The letter 'z' appears at most 2 times in a row. |
| 16 | +Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString. |
| 17 | +The string is the lexicographically largest repeatLimitedString possible so we return "zzcccac". |
| 18 | +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. |
| 19 | +</pre> |
| 20 | + |
| 21 | +<p><strong>Example 2:</strong></p> |
| 22 | + |
| 23 | +<pre><strong>Input:</strong> s = "aababab", repeatLimit = 2 |
| 24 | +<strong>Output:</strong> "bbabaa" |
| 25 | +<strong>Explanation:</strong> We use only some of the characters from s to construct the repeatLimitedString "bbabaa". |
| 26 | +The letter 'a' appears at most 2 times in a row. |
| 27 | +The letter 'b' appears at most 2 times in a row. |
| 28 | +Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString. |
| 29 | +The string is the lexicographically largest repeatLimitedString possible so we return "bbabaa". |
| 30 | +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. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p> </p> |
| 34 | +<p><strong>Constraints:</strong></p> |
| 35 | + |
| 36 | +<ul> |
| 37 | + <li><code>1 <= repeatLimit <= s.length <= 10<sup>5</sup></code></li> |
| 38 | + <li><code>s</code> consists of lowercase English letters.</li> |
| 39 | +</ul> |
| 40 | +</div> |
0 commit comments