Minimize run-length encoded length with deletions
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string manipulation, run-length encoding concepts, and dynamic programming-based optimization under deletion constraints.
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.
Input: ('AAAAAAAAAA', 1)
Expected Output: 2
Explanation: Without deletion, AAAAAAAAAA compresses to A10 with length 3. Deleting one A gives A9 with length 2.
Input: ('AAABA', 1)
Expected Output: 2
Explanation: Delete B to merge the runs into AAAA, which compresses to A4 with length 2.
Input: ('AABCCC', 0)
Expected Output: 5
Explanation: With no deletions, AABCCC compresses to A2BC3, whose length is 5.
Hints
- Use dynamic programming on suffixes. At each position, either delete the current character or keep it as the start of the next encoded run.
- When you keep s[i], try extending its run to the right while deleting non-matching characters. The cost of a run depends only on how many copies you keep.