|
| 1 | +3003\. Maximize the Number of Partitions After Operations |
| 2 | + |
| 3 | +Hard |
| 4 | + |
| 5 | +You are given a **0-indexed** string `s` and an integer `k`. |
| 6 | + |
| 7 | +You are to perform the following partitioning operations until `s` is **empty**: |
| 8 | + |
| 9 | +* Choose the **longest** **prefix** of `s` containing at most `k` **distinct** characters. |
| 10 | +* **Delete** the prefix from `s` and increase the number of partitions by one. The remaining characters (if any) in `s` maintain their initial order. |
| 11 | + |
| 12 | +**Before** the operations, you are allowed to change **at most** **one** index in `s` to another lowercase English letter. |
| 13 | + |
| 14 | +Return _an integer denoting the **maximum** number of resulting partitions after the operations by optimally choosing at most one index to change._ |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +**Input:** s = "accca", k = 2 |
| 19 | + |
| 20 | +**Output:** 3 |
| 21 | + |
| 22 | +**Explanation:** In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'. s becomes "acbca". The operations can now be performed as follows until s becomes empty: |
| 23 | +- Choose the longest prefix containing at most 2 distinct characters, "<ins>ac</ins>bca". |
| 24 | +- Delete the prefix, and s becomes "bca". The number of partitions is now 1. |
| 25 | +- Choose the longest prefix containing at most 2 distinct characters, "<ins>bc</ins>a". |
| 26 | +- Delete the prefix, and s becomes "a". The number of partitions is now 2. |
| 27 | +- Choose the longest prefix containing at most 2 distinct characters, "<ins>a</ins>". |
| 28 | +- Delete the prefix, and s becomes empty. The number of partitions is now 3. |
| 29 | + |
| 30 | +Hence, the answer is 3. It can be shown that it is not possible to obtain more than 3 partitions. |
| 31 | + |
| 32 | +**Example 2:** |
| 33 | + |
| 34 | +**Input:** s = "aabaab", k = 3 |
| 35 | + |
| 36 | +**Output:** 1 |
| 37 | + |
| 38 | +**Explanation:** In this example, to maximize the number of resulting partitions we can leave s as it is. The operations can now be performed as follows until s becomes empty: |
| 39 | +- Choose the longest prefix containing at most 3 distinct characters, "<ins>aabaab</ins>". |
| 40 | +- Delete the prefix, and s becomes empty. The number of partitions becomes 1. |
| 41 | + |
| 42 | +Hence, the answer is 1. It can be shown that it is not possible to obtain more than 1 partition. |
| 43 | + |
| 44 | +**Example 3:** |
| 45 | + |
| 46 | +**Input:** s = "xxyz", k = 1 |
| 47 | + |
| 48 | +**Output:** 4 |
| 49 | + |
| 50 | +**Explanation:** In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'. s becomes "xayz". The operations can now be performed as follows until s becomes empty: |
| 51 | +- Choose the longest prefix containing at most 1 distinct character, "<ins>x</ins>ayz". |
| 52 | +- Delete the prefix, and s becomes "ayz". The number of partitions is now 1. |
| 53 | +- Choose the longest prefix containing at most 1 distinct character, "<ins>a</ins>yz". |
| 54 | +- Delete the prefix, and s becomes "yz". The number of partitions is now 2. |
| 55 | +- Choose the longest prefix containing at most 1 distinct character, "<ins>y</ins>z". |
| 56 | +- Delete the prefix, and s becomes "z". The number of partitions is now 3. |
| 57 | +- Choose the longest prefix containing at most 1 distinct character, "<ins>z</ins>". |
| 58 | +- Delete the prefix, and s becomes empty. The number of partitions is now 4. |
| 59 | + |
| 60 | +Hence, the answer is 4. It can be shown that it is not possible to obtain more than 4 partitions. |
| 61 | + |
| 62 | +**Constraints:** |
| 63 | + |
| 64 | +* <code>1 <= s.length <= 10<sup>4</sup></code> |
| 65 | +* `s` consists only of lowercase English letters. |
| 66 | +* `1 <= k <= 26` |
0 commit comments