Leetcode #1531: String Compression II
In this guide, we solve Leetcode #1531 String Compression II in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

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".
Quick Facts
- Difficulty: Hard
- Premium: No
- Tags: String, Dynamic Programming
Intuition
The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.
A carefully chosen DP state captures exactly what we need to build the final answer.
Approach
Define the DP state and recurrence, then compute states in the correct order.
Optionally compress space once the recurrence is clear.
Steps:
- Choose a DP state definition.
- Write the recurrence and base cases.
- Compute states in the correct order.
Example
Input: s = "aaabcccd", k = 2
Output: 4
Explanation: Compressing s without deleting anything will give us "a3bc3d" of length 6. Deleting any of the characters 'a' or 'c' would at most decrease the length of the compressed string to 5, for instance delete 2 'a' then we will have s = "abcccd" which compressed is abc3d. Therefore, the optimal way is to delete 'b' and 'd', then the compressed version of s will be "a3c3" of length 4.
Python Solution
from functools import lru_cache
def getLengthOfOptimalCompression(s: str, k: int) -> int:
n = len(s)
def dp(i: int, last: str, run: int, k_left: int) -> int:
if k_left < 0:
return 10**9
if i == n:
return 0
# Option 1: delete s[i]
best = dp(i + 1, last, run, k_left - 1)
# Option 2: keep s[i]
if s[i] == last:
inc = 1 if run in (1, 9, 99) else 0
best = min(best, inc + dp(i + 1, last, run + 1, k_left))
else:
best = min(best, 1 + dp(i + 1, s[i], 1, k_left))
return best
return dp(0, '#', 0, k)
Complexity
The time complexity is O(n·m) (typical). The space complexity is O(n·m) or optimized.
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.