Please read the new rule regarding the restriction on the use of AI tools. ×

Maximum deletions in a string

Revision en3, by neo_30, 2023-10-29 11:56:22

Given a string s and integer K. You start creating a substring from the start until there are at most K distinct characters. You must choose the largest such substring and delete it. Again you do the same thing until the entire string is deleted.
Also, you can replace at most one character of the string with any other character of your choice.

You have to find the maximum number of such deletions you can achieve.

$$$s = "aaa", K = 1 $$$
$$$ans = 3$$$
Explanation: Initially we could only achieve 1 deletion, but if you make $$$s = aba$$$, now we can have 3 deletions.

$$$s.size()<=10^4$$$
$$$1<=K<=26$$$
s contains only lowercase English letters.


Please help me understand how to achieve an optimal solution to this question. Thanks!

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English neo_30 2023-10-29 11:58:37 4
en3 English neo_30 2023-10-29 11:56:22 24
en2 English neo_30 2023-10-29 11:48:33 50 Tiny change: '1 deletion but if yo' -> '1 deletion, but if yo'
en1 English neo_30 2023-10-29 11:46:55 733 Initial revision (published)