3003. Maximize the Number of Partitions After Operations
Description
You are given a string s and an integer k.
First, you are allowed to change at most one index in s to another lowercase English letter.
After that, do the following partitioning operation until s is empty:
- Choose the longest prefix of
scontaining at mostkdistinct characters. - Delete the prefix from
sand increase the number of partitions by one. The remaining characters (if any) insmaintain their initial order.
Return an integer denoting the maximum number of resulting partitions after the operations by optimally choosing at most one index to change.
Example 1:
Input: s = "accca", k = 2
Output: 3
Explanation:
The optimal way is to change s[2] to something other than a and c, for example, b. then it becomes "acbca".
Then we perform the operations:
- The longest prefix containing at most 2 distinct characters is
"ac", we remove it andsbecomes"bca". - Now The longest prefix containing at most 2 distinct characters is
"bc", so we remove it andsbecomes"a". - Finally, we remove
"a"andsbecomes empty, so the procedure ends.
Doing the operations, the string is divided into 3 partitions, so the answer is 3.
Example 2:
Input: s = "aabaab", k = 3
Output: 1
Explanation:
Initially s 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.
Example 3:
Input: s = "xxyz", k = 1
Output: 4
Explanation:
The optimal way is to change s[0] or s[1] to something other than characters in s, for example, to change s[0] to w.
Then s becomes "wxyz", which consists of 4 distinct characters, so as k is 1, it will divide into 4 partitions.
Constraints:
1 <= s.length <= 104sconsists only of lowercase English letters.1 <= k <= 26
Solutions
Solution 1: Memoized Search
We design a function \(\textit{dfs}(i, \textit{cur}, t)\) that represents the maximum number of partitions we can obtain when currently processing index \(i\) of string \(s\), the current prefix already contains the character set \(\textit{cur}\), and we can still modify \(t\) characters. Then the answer is \(\textit{dfs}(0, 0, 1)\).
The execution logic of function \(\textit{dfs}(i, \textit{cur}, t)\) is as follows:
- If \(i \geq n\), it means we have finished processing string \(s\), return 1.
- Calculate the bitmask \(v = 1 \ll (s[i] - 'a')\) corresponding to the current character \(s[i]\), and calculate the updated character set \(\textit{nxt} = \textit{cur} \mid v\).
- If the number of bits in \(\textit{nxt}\) exceeds \(k\), it means the current prefix already contains more than \(k\) distinct characters. We need to make a partition, increment the partition count by 1, and recursively call \(\textit{dfs}(i + 1, v, t)\). Otherwise, continue recursively calling \(\textit{dfs}(i + 1, \textit{nxt}, t)\).
- If \(t > 0\), it means we can still modify a character once. We try to change the current character \(s[i]\) to any lowercase letter (26 choices in total). For each choice, calculate the updated character set \(\textit{nxt} = \textit{cur} \mid (1 \ll j)\), and based on whether it exceeds \(k\) distinct characters, choose the corresponding recursive call method to update the maximum partition count.
- Use a hash table to cache already computed states to avoid redundant calculations.
The time complexity is \(O(n \times |\Sigma| \times k)\) and the space complexity is \(O(n \times |\Sigma| \times k)\), where \(n\) is the length of string \(s\), and \(|\Sigma|\) is the size of the character set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |