String Compression II - Problem
String Compression II presents an advanced optimization challenge involving run-length encoding with strategic character deletion.
You're given a string
•
•
Single characters remain unchanged (no '1' is added). Your goal is to find the minimum possible length of the run-length encoded string after optimally deleting up to
Key insight: Sometimes deleting characters creates longer runs, which can result in shorter overall encoding!
You're given a string
s and can delete at most k characters to minimize the compressed string's length. Run-length encoding replaces consecutive identical characters (2+ occurrences) with the character followed by its count. For example:•
"aabccc" → "a2bc3" (length 5)•
"aaabcccc" → "a3bc4" (length 5)Single characters remain unchanged (no '1' is added). Your goal is to find the minimum possible length of the run-length encoded string after optimally deleting up to
k characters.Key insight: Sometimes deleting characters creates longer runs, which can result in shorter overall encoding!
Input & Output
example_1.py — Basic Optimization
$
Input:
s = "aaabcccc", k = 2
›
Output:
4
💡 Note:
We can delete the 'b' and one 'c' to get "aaacc" which becomes "a3c2" (length 4). Alternatively, delete positions to create "aaacccc" → "a3c4" (length 4).
example_2.py — No Deletions Needed
$
Input:
s = "aabbaa", k = 3
›
Output:
2
💡 Note:
We can delete 'bb' and one 'a' to get "aaa" which becomes "a3" (length 2). This is optimal since we're creating the longest possible run.
example_3.py — Edge Case
$
Input:
s = "aaaaaaaaaaa", k = 0
›
Output:
3
💡 Note:
Cannot delete any characters. The string "aaaaaaaaaaa" (11 a's) becomes "a11" which has length 3 (character + two digits).
Visualization
Tap to expand
Understanding the Visualization
1
Analyze Runs
Identify consecutive character groups and their compression potential
2
Strategic Deletion
Delete characters to merge distant runs of the same character
3
Count Encoding
Apply run-length encoding rules: single=1, 2-9=2chars, 10-99=3chars, 100+=4chars
4
Minimize Length
Find the deletion strategy that produces the shortest encoded result
Key Takeaway
🎯 Key Insight: The optimal solution uses dynamic programming to explore all deletion possibilities while memoizing results. Strategic character removal can merge distant character groups into longer runs, ultimately producing shorter compressed strings than keeping all characters.
Time & Space Complexity
Time Complexity
O(n * k * n * n)
n positions × k deletions × n possible characters × n possible counts, with memoization
✓ Linear Growth
Space Complexity
O(n * k * n * n)
Memoization table stores states for position, deletions, character, and count
⚡ Linearithmic Space
Constraints
- 1 ≤ s.length ≤ 100
- 0 ≤ k ≤ s.length
- s contains only lowercase English letters
- The goal is to minimize the run-length encoded string length
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code