Skip to content

Commit 2b303d1

Browse files
committed
Create README - LeetHub
1 parent 01872b0 commit 2b303d1

File tree

1 file changed

+70
-0
lines changed
  • 3233-maximize-the-number-of-partitions-after-operations

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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>&nbsp;</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 = &quot;accca&quot;, 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>&quot;acbca&quot;</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>&quot;ac&quot;</code>, we remove it and <code>s</code> becomes <code>&quot;bca&quot;</code>.</li>
30+
<li>Now The longest prefix containing at most 2 distinct characters is <code>&quot;bc&quot;</code>, so we remove it and <code>s</code> becomes <code>&quot;a&quot;</code>.</li>
31+
<li>Finally, we remove <code>&quot;a&quot;</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 = &quot;aabaab&quot;, 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&nbsp;<code>s</code>&nbsp;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 = &quot;xxyz&quot;, 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&nbsp;<code>s[0]</code>&nbsp;or&nbsp;<code>s[1]</code>&nbsp;to something other than characters in&nbsp;<code>s</code>, for example, to change&nbsp;<code>s[0]</code>&nbsp;to&nbsp;<code>w</code>.</p>
59+
60+
<p>Then&nbsp;<code>s</code>&nbsp;becomes <code>&quot;wxyz&quot;</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>&nbsp;</p>
64+
<p><strong>Constraints:</strong></p>
65+
66+
<ul>
67+
<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
68+
<li><code>s</code> consists only of lowercase English letters.</li>
69+
<li><code>1 &lt;= k &lt;= 26</code></li>
70+
</ul>

0 commit comments

Comments
 (0)