Skip to content

Practice Maximize the Number of Partitions After Operations

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3003
LeetCode Problem

Maximize the Number of Partitions After Operations

Problem Statement

Given a lowercase string s and an integer k, you may first change at most one character to another lowercase letter. Then repeatedly remove the longest remaining prefix containing at most k distinct characters, counting each removed prefix as a partition. Preserve the order of the remaining characters. Return the largest possible partition count over your optional character change.

Source: Maximize the Number of Partitions After Operations on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "accca", k = 2
Output
3

# Example 2

Input
s = "aabaab", k = 3
Output
1

# Example 3

Input
s = "xxyz", k = 1
Output
4

Constraints

1 <= s.length <= 10^4
s consists only of lowercase English letters.
1 <= k <= 26

Related LeetCode Problems

Practice more problems similar to Maximize the Number of Partitions After Operations.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.