Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Maximum deletions in a string

Правка en2, от neo_30, 2023-10-29 11:48:33

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 delete the largest such substring. 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!

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en4 Английский neo_30 2023-10-29 11:58:37 4
en3 Английский neo_30 2023-10-29 11:56:22 24
en2 Английский neo_30 2023-10-29 11:48:33 50 Tiny change: '1 deletion but if yo' -> '1 deletion, but if yo'
en1 Английский neo_30 2023-10-29 11:46:55 733 Initial revision (published)