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.
1#include <stdio.h>
2#include <string.h>
3#include <limits.h>
4
5int compressLength(int c) {
6 if (c == 0) return 0;
7 if (c == 1) return 1;
8 if (c < 10) return 2;
9 if (c < 100) return 3;
10 return 4;
11}
12
13int minLength(char* s, int k) {
14 int n = strlen(s);
15 int dp[n+1][k+1];
16 for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) dp[i][j] = INT_MAX;
17 dp[0][0] = 0;
18 for (int i = 0; i < n; i++) {
19 for (int j = 0; j <= k; j++) {
20 if (dp[i][j] == INT_MAX) continue;
21 int cnt = 0;
22 for (int l = i; l < n; l++) {
23 if (s[l] == s[i]) cnt++;
24 int needed = l - i + 1 - cnt;
25 if (j + needed <= k) {
26 dp[l+1][j+needed] = fmin(dp[l+1][j+needed], dp[i][j] + compressLength(cnt));
27 }
28 }
29 }
30 }
31 int result = INT_MAX;
32 for (int j = 0; j <= k; j++) result = fmin(result, dp[n][j]);
33 return result;
34}
35
36int main() {
37 char s[] = "aaabcccd";
38 int k = 2;
39 int result = minLength(s, k);
40 printf("%d\n", result);
41 return 0;
42}
The solution defines a helper function compressLength
which calculates the length of the compressed representation given a count. The main function minLength
initializes a DP array and iteratively fills it by considering each character, counting contiguous appearances, and updating the DP state to find the minimum compression lengths achievable with allowed deletions.
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 <iostream>
2#include <vector>
3#include <algorithm>
4using namespace std;
5
6int minLength(const string& s, int k) {
7 vector<int> freq(26, 0);
8 for (char c : s) freq[c - 'a']++;
9
10 sort(freq.rbegin(), freq.rend());
11 for (int i = 0; i < 26 && k > 0; i++) {
12 if (freq[i] > 0) {
13 int deleteCount = freq[i] - (freq[i] == 1 ? 0 : 1);
14 deleteCount = min(deleteCount, k);
15 freq[i] -= deleteCount;
16 k -= deleteCount;
17 }
18 }
19
20 int length = 0;
21 for (int f : freq) {
22 if (f > 0) {
23 length += f == 1 ? 1 : 1 + (f < 10 ? 2 : f < 100 ? 3 : 4);
24 }
25 }
26
27 return length;
28}
29
30int main() {
31 string s = "aaabcccd";
32 int k = 2;
33 cout << minLength(s, k) << endl;
34 return 0;
35}
The C++ program computes minimum length by using a greedy approach on frequency counts. By sorting and then reducing the highest frequencies, we simplify the calculation of the final compressed length. We make use of the STL to sort and calculate efficiently.