This approach uses dynamic programming to minimize the length of the compressed string after removing up to k characters. We'll iteratively decide for each position in the string whether to remove the character or keep it, and if kept, determine the optimal compression length.
The idea is to use a DP table where dp[i][j] stores the minimal compression length achievable for the substring s[0...i] with j deletions.
Time Complexity: O(n^3). For each starting point, we may need to process every subsequent character and make decisions up to k deletions.
Space Complexity: O(n*k). Space used by the DP table.
1import java.util.Arrays;
2
3public class StringCompression {
4 private static int compressLength(int count) {
5 if (count == 0) return 0;
6 if (count == 1) return 1;
7 if (count < 10) return 2;
8 if (count < 100) return 3;
9 return 4;
10 }
11
12 public static int minLength(String s, int k) {
13 int n = s.length();
14 int[][] dp = new int[n + 1][k + 1];
15 for (int[] row : dp) Arrays.fill(row, Integer.MAX_VALUE);
16 dp[0][0] = 0;
17 for (int i = 0; i < n; i++) {
18 for (int j = 0; j <= k; j++) {
19 if (dp[i][j] == Integer.MAX_VALUE) continue;
20 int count = 0;
21 for (int l = i; l < n; l++) {
22 if (s.charAt(l) == s.charAt(i)) count++;
23 int needDelete = l - i + 1 - count;
24 if (j + needDelete <= k) {
25 dp[l + 1][j + needDelete] = Math.min(dp[l + 1][j + needDelete], dp[i][j] + compressLength(count));
26 }
27 }
28 }
29 }
30 return Arrays.stream(dp[n]).min().getAsInt();
31 }
32
33 public static void main(String[] args) {
34 String s = "aaabcccd";
35 int k = 2;
36 System.out.println(minLength(s, k));
37 }
38}
The Java solution utilizes similar logic to C and C++, with the addition of the Arrays
class for initializing the DP table and determining the minimum length efficiently. This solution focuses on iterations over the string to update DP states as needed.
In this greedy approach, we identify segments of identical characters and evaluate them individually, considering their entire group in the adjustments. By counting the length of these segments, we can make informed decisions about deletions that do not depend on the widely traversed DP table, reducing some overhead complexity.
Time Complexity: O(n + m log m) where n is the length of the string and m = 26 (constant time, simplified in this case).
Space Complexity: O(m) for frequency arrays.
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4
5int cmpint(const void *a, const void *b) {
6 return (*(int*)b - *(int*)a);
7}
8
9int minLength(char *s, int k) {
10 int n = strlen(s);
11 int freq[26] = {0}, maxFreq[26] = {0};
12
13 for (int i = 0; i < n; i++) freq[s[i] - 'a']++;
14 memcpy(maxFreq, freq, sizeof(freq));
15
16 qsort(freq, 26, sizeof(int), cmpint);
17
18 for (int i = 0; i < 26 && k > 0; i++) {
19 if (freq[i] > 0) {
20 int deleteCount = freq[i] - (freq[i] == 1 ? 0 : 1);
21 deleteCount = deleteCount < k ? deleteCount : k;
22 freq[i] -= deleteCount;
23 k -= deleteCount;
24 }
25 }
26
27 int length = 0;
28 for (int i = 0; i < 26; i++) {
29 if (freq[i] > 0) {
30 length += freq[i] == 1 ? 1 : 1 + (freq[i] < 10 ? 2 : freq[i] < 100 ? 3 : 4);
31 }
32 }
33
34 return length;
35}
36
37int main() {
38 char s[] = "aaabcccd";
39 int k = 2;
40 printf("%d\n", minLength(s, k));
41 return 0;
42}
This C solution uses a frequency table to count occurrences of each character and adjusts the counts after sorting them. By decreasing the most frequent occurrences, we fit within the limit of deletions. The resulting array gives us a simple method to calculate the resulting minimal compressed length.