Maximize the Number of Partitions After Operations Problem
Description
LeetCode Problem 3003.
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:
1. The longest prefix containing at most 2 distinct characters is "ac", we remove it and s becomes "bca".
2. Now The longest prefix containing at most 2 distinct characters is "bc", so we remove it and s becomes "a".
3. Finally, we remove "a" and s becomes 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 <= 10^4sconsists only of lowercase English letters.1 <= k <= 26
Solution
Approach: Memoized Search
Greedy partitioning cuts the string whenever the next character would make the current piece contain more than k distinct letters. We may change at most one character, so use a search over (i, cur, t): the index, the bitmask of letters in the current piece, and whether the change is still available.
At index i, process s[i] unchanged: if adding it keeps the piece within k letters, continue with the merged mask; otherwise start a new piece (+1 partition) with just that letter. If the change is still available, also try every letter j in place of s[i] the same way, using t = 0 afterwards. The answer is dfs(0, 0, 1), and reaching the end counts as 1 piece. Memoize the states.
Complexity: About O(26 * n * (number of reachable masks)) time and the same space; masks are small because a piece holds at most k letters.
from functools import lru_cache
class Solution:
def maxPartitionsAfterOperations(self, s: str, k: int) -> int:
@lru_cache(None)
def dfs(i: int, cur: int, t: int) -> int:
if i >= n:
return 1
v = 1 << (ord(s[i]) - ord("a"))
nxt = cur | v
if bin(nxt).count('1') > k:
ans = dfs(i + 1, v, t) + 1
else:
ans = dfs(i + 1, nxt, t)
if t:
for j in range(26):
nxt = cur | (1 << j)
if bin(nxt).count('1') > k:
ans = max(ans, dfs(i + 1, 1 << j, 0) + 1)
else:
ans = max(ans, dfs(i + 1, nxt, 0))
return ans
n = len(s)
return dfs(0, 0, 1)