#3003

Maximize the Number of Partitions After Operations

candidate master · 1350 · lc hard +32 · 53.6% accepted · 435 likes · top 45%

Description

You are given a string s and an integer k.

You may change at most one character in s to another lowercase English letter.

Then, repeatedly perform this partition: choose the longest prefix of s containing at most k distinct characters, delete it, and increment the partition count. Continue until s is empty.

Return the maximum number of partitions achievable by optimally choosing at most one character to change.

Code

1
2
3