Minimize run-length encoded length with deletions
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
### Problem
You are given a string `s` (uppercase English letters) and an integer `k`.
You may delete **at most `k` characters** from `s`. After deletions, you compress the resulting string using **run-length encoding (RLE)**:
- Consecutive equal characters are replaced by the character followed by the run length **only if the run length > 1**.
- Examples:
- `"A" -> "A"`
- `"AA" -> "A2"`
- `"AAA" -> "A3"`
- `"AABCCC" -> "A2BC3"`
### Task
Return the **minimum possible length** of the RLE-compressed string after deleting at most `k` characters.
### Input / Output
- Input: string `s`, integer `k`
- Output: integer (minimum compressed length)
### Constraints (typical interview bounds)
- `1 <= len(s) <= 100`
- `0 <= k <= len(s)`
### Clarifications
- The “length” is the number of characters in the encoded string (e.g., `"A12"` has length 3).
Quick Answer: This question evaluates proficiency in string manipulation, run-length encoding concepts, and dynamic programming-based optimization under deletion constraints.
You are given a string s consisting of uppercase English letters and an integer k. You may delete at most k characters from s. After deletions, compress the remaining string using run-length encoding (RLE): each maximal block of consecutive equal characters becomes the character followed by its count only when the count is greater than 1. For example, A becomes A, AA becomes A2, and AABCCC becomes A2BC3. Return the minimum possible length of the compressed string. If all characters are deleted, the compressed length is 0.
Constraints
- 1 <= len(s) <= 100
- 0 <= k <= len(s)
- s consists only of uppercase English letters
Examples
Input: ('AAABCCCD', 2)
Expected Output: 4
Explanation: Delete B and D to get AAACCC, which compresses to A3C3 with length 4.
Input: ('AABBA', 2)
Expected Output: 2
Explanation: Delete both B characters to get AAA, which compresses to A3 with length 2.
Input: ('A', 0)
Expected Output: 1
Explanation: No deletions are allowed. A compresses to A, so the length is 1.
Input: ('ABCDE', 5)
Expected Output: 0
Explanation: You may delete all characters, leaving an empty string with compressed length 0.