java.lang.Object
g2101_2200.s2182_construct_string_with_repeat_limit.Solution

public class Solution extends Object
2182 - Construct String With Repeat Limit.<p>Medium</p> <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> <p>Return <em>the <strong>lexicographically largest</strong></em> <code>repeatLimitedString</code> <em>possible</em>.</p> <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> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;cczazcc&rdquo;, repeatLimit = 3</p> <p><strong>Output:</strong> &ldquo;zzcccac&rdquo;</p> <p><strong>Explanation:</strong> We use all of the characters from s to construct the repeatLimitedString &ldquo;zzcccac&rdquo;.</p> <p>The letter &lsquo;a&rsquo; appears at most 1 time in a row.</p> <p>The letter &lsquo;c&rsquo; appears at most 3 times in a row.</p> <p>The letter &lsquo;z&rsquo; appears at most 2 times in a row.</p> <p>Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.</p> <p>The string is the lexicographically largest repeatLimitedString possible so we return &ldquo;zzcccac&rdquo;.</p> <p>Note that the string &ldquo;zzcccca&rdquo; is lexicographically larger but the letter &lsquo;c&rsquo; appears more than 3 times in a row, so it is not a valid repeatLimitedString.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;aababab&rdquo;, repeatLimit = 2</p> <p><strong>Output:</strong> &ldquo;bbabaa&rdquo;</p> <p><strong>Explanation:</strong> We use only some of the characters from s to construct the repeatLimitedString &ldquo;bbabaa&rdquo;.</p> <p>The letter &lsquo;a&rsquo; appears at most 2 times in a row. The letter &lsquo;b&rsquo; appears at most 2 times in a row.</p> <p>Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.</p> <p>The string is the lexicographically largest repeatLimitedString possible so we return &ldquo;bbabaa&rdquo;.</p> <p>Note that the string &ldquo;bbabaaa&rdquo; is lexicographically larger but the letter &lsquo;a&rsquo; appears more than 2 times in a row, so it is not a valid repeatLimitedString.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= repeatLimit <= s.length <= 10<sup>5</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • repeatLimitedString

      public String repeatLimitedString(String s, int repeatLimit)