Skip to content

Practice String Compression II

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

Hard#1531
LeetCode Problem

String Compression II

Problem Statement

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 (length of the run). For example, to compress the string "aabccc" we replace "aa" by "a2" and replace "ccc" by "c3". Thus the compressed string becomes "a2bc3". Notice that in this problem, we are not adding '1' after single characters. Given a string s and an integer k. You need to delete at most k characters from s such that the run-length encoded version of s has minimum length. Find the minimum length of the run-length encoded version of s after deleting at most k characters.

Source: String Compression II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "aaabcccd", k = 2
Output
4

# Example 2

Input
s = "aabbaa", k = 2
Output
2

# Example 3

Input
s = "aaaaaaaaaaa", k = 0
Output
3

Constraints

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

Related LeetCode Problems

Practice more problems similar to String Compression II.

Review your practice with PhantomCodeAI

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