|
| 1 | +<h2><a href="https://leetcode.com/problems/maximize-the-number-of-partitions-after-operations">3233. Maximize the Number of Partitions After Operations</a></h2><h3>Hard</h3><hr><p>You are given a string <code>s</code> and an integer <code>k</code>.</p> |
| 2 | + |
| 3 | +<p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> |
| 4 | + |
| 5 | +<p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</strong>:</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | + <li>Choose the <strong>longest</strong> <strong>prefix</strong> of <code>s</code> containing at most <code>k</code> <strong>distinct</strong> characters.</li> |
| 9 | + <li><strong>Delete</strong> the prefix from <code>s</code> and increase the number of partitions by one. The remaining characters (if any) in <code>s</code> maintain their initial order.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>Return an integer denoting the <strong>maximum</strong> number of resulting partitions after the operations by optimally choosing at most one index to change.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<div class="example-block"> |
| 18 | +<p><strong>Input:</strong> <span class="example-io">s = "accca", k = 2</span></p> |
| 19 | + |
| 20 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 21 | + |
| 22 | +<p><strong>Explanation:</strong></p> |
| 23 | + |
| 24 | +<p>The optimal way is to change <code>s[2]</code> to something other than a and c, for example, b. then it becomes <code>"acbca"</code>.</p> |
| 25 | + |
| 26 | +<p>Then we perform the operations:</p> |
| 27 | + |
| 28 | +<ol> |
| 29 | + <li>The longest prefix containing at most 2 distinct characters is <code>"ac"</code>, we remove it and <code>s</code> becomes <code>"bca"</code>.</li> |
| 30 | + <li>Now The longest prefix containing at most 2 distinct characters is <code>"bc"</code>, so we remove it and <code>s</code> becomes <code>"a"</code>.</li> |
| 31 | + <li>Finally, we remove <code>"a"</code> and <code>s</code> becomes empty, so the procedure ends.</li> |
| 32 | +</ol> |
| 33 | + |
| 34 | +<p>Doing the operations, the string is divided into 3 partitions, so the answer is 3.</p> |
| 35 | +</div> |
| 36 | + |
| 37 | +<p><strong class="example">Example 2:</strong></p> |
| 38 | + |
| 39 | +<div class="example-block"> |
| 40 | +<p><strong>Input:</strong> <span class="example-io">s = "aabaab", k = 3</span></p> |
| 41 | + |
| 42 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 43 | + |
| 44 | +<p><strong>Explanation:</strong></p> |
| 45 | + |
| 46 | +<p>Initially <code>s</code> contains 2 distinct characters, so whichever character we change, it will contain at most 3 distinct characters, so the longest prefix with at most 3 distinct characters would always be all of it, therefore the answer is 1.</p> |
| 47 | +</div> |
| 48 | + |
| 49 | +<p><strong class="example">Example 3:</strong></p> |
| 50 | + |
| 51 | +<div class="example-block"> |
| 52 | +<p><strong>Input:</strong> <span class="example-io">s = "xxyz", k = 1</span></p> |
| 53 | + |
| 54 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 55 | + |
| 56 | +<p><strong>Explanation:</strong></p> |
| 57 | + |
| 58 | +<p>The optimal way is to change <code>s[0]</code> or <code>s[1]</code> to something other than characters in <code>s</code>, for example, to change <code>s[0]</code> to <code>w</code>.</p> |
| 59 | + |
| 60 | +<p>Then <code>s</code> becomes <code>"wxyz"</code>, which consists of 4 distinct characters, so as <code>k</code> is 1, it will divide into 4 partitions.</p> |
| 61 | +</div> |
| 62 | + |
| 63 | +<p> </p> |
| 64 | +<p><strong>Constraints:</strong></p> |
| 65 | + |
| 66 | +<ul> |
| 67 | + <li><code>1 <= s.length <= 10<sup>4</sup></code></li> |
| 68 | + <li><code>s</code> consists only of lowercase English letters.</li> |
| 69 | + <li><code>1 <= k <= 26</code></li> |
| 70 | +</ul> |
0 commit comments