String Compression II

Run-length encoding is a string compression method that works by replacing consecutive identical characters repeated 2 or more times with the concatenation of the character and the number marking the count of the characters, which is the length of the run.

Notice that in this problem, we are not adding '1' after single characters.

Given a string s and an integer k, delete at most k characters from s such that the run-length encoded version of s has minimum length.

Return the minimum length of the run-length encoded version of s after deleting at most k characters.

Example 1
Inputs = "aaabcccd", k = 2
Output4
Deleting b and d makes the compressed version "a3c3", which has length 4 and is optimal.
Example 2
Inputs = "aabbaa", k = 2
Output2
Deleting both b characters leaves "aaaa", whose compressed form is "a4" with length 2.

Constraints

  • 1 <= s.length <= 100
  • 0 <= k <= s.length
  • s contains only lowercase English letters.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate