-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstring_compression_II.dart
312 lines (270 loc) · 9.06 KB
/
string_compression_II.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
-* String Compression II *-
Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string "aabccc" we replace "aa" by "a2" and replace "ccc" by "c3". Thus the compressed string becomes "a2bc3".
Notice that in this problem, we are not adding '1' after single characters.
Given a string s and an integer k. You need to delete at most k characters from s such that the run-length encoded version of s has minimum length.
Find the minimum length of the run-length encoded version of s after deleting at most k characters.
Example 1:
Input: s = "aaabcccd", k = 2
Output: 4
Explanation: Compressing s without deleting anything will give us "a3bc3d" of length 6. Deleting any of the characters 'a' or 'c' would at most decrease the length of the compressed string to 5, for instance delete 2 'a' then we will have s = "abcccd" which compressed is abc3d. Therefore, the optimal way is to delete 'b' and 'd', then the compressed version of s will be "a3c3" of length 4.
Example 2:
Input: s = "aabbaa", k = 2
Output: 2
Explanation: If we delete both 'b' characters, the resulting compressed string would be "a4" of length 2.
Example 3:
Input: s = "aaaaaaaaaaa", k = 0
Output: 3
Explanation: Since k is zero, we cannot delete anything. The compressed string is "a11" of length 3.
Constraints:
1 <= s.length <= 100
0 <= k <= s.length
s contains only lowercase English letters.
*/
import 'dart:collection';
import 'dart:math';
class A {
// Runtime: 345 ms, faster than 100.00% of Dart online submissions for String Compression II.
// Memory Usage: 161.4 MB, less than 100.00% of Dart online submissions for String Compression II.
int getLengthOfOptimalCompression(String s, int k) {
// dp[i][k]: the minimum length for s[:i] with at most k deletion.
int n = s.length;
List<List<int>> dp =
List.filled(110, 0).map((e) => List.filled(110, 0)).toList();
for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) dp[i][j] = 9999;
// for (int[] i : dp) Arrays.fill(i, n); // this is a bit slower (100ms)
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= k; j++) {
int cnt = 0, del = 0;
for (int l = i; l >= 1; l--) {
// keep s[i], concate the same, remove the different
// if (s.charAt(l - 1) == s.charAt(i - 1))
if (s.codeUnitAt(l - 1) == s.codeUnitAt(i - 1))
cnt++;
else
del++;
if (j - del >= 0)
dp[i][j] = min(
dp[i][j],
dp[l - 1][j - del] +
1 +
(cnt >= 100
? 3
: cnt >= 10
? 2
: cnt >= 2
? 1
: 0));
}
if (j > 0) // delete s[i]
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
}
}
return dp[n][k];
}
}
class C {
int getLengthOfOptimalCompression(String s, int k) {
int n = s.length;
// List<List<int>> dp = int[n + 1][k + 1];
List<List<int>> dp =
List.filled(n + 1, 0).map((e) => List.filled(k + 1, 0)).toList();
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= k; j++) {
dp[i][j] = 10000;
}
}
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= k; j++) {
int count = 0;
int delete = 0;
for (int m = i; m >= 1; m--) {
if (s.codeUnitAt(m - 1) == s.codeUnitAt(i - 1)) {
count++;
} else {
delete++;
}
if (j - delete >= 0) {
dp[i][j] = min(
dp[i][j],
dp[m - 1][j - delete] +
1 +
(count >= 100
? 3
: count >= 10
? 2
: count >= 2
? 1
: 0));
}
}
if (j > 0) {
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
}
}
}
return dp[n][k];
}
}
class Solution {
// Runtime: 534 ms, faster than 100.00% of Dart online submissions for String Compression II.
// Memory Usage: 161.9 MB, less than 100.00% of Dart online submissions for String Compression II.
int n = 127;
late List<List<int>> dp;
int getLen(int x) {
return x == 1
? 0
: x < 10
? 1
: x < 100
? 2
: 3;
}
int helper(String str, int left, int k) {
if (k < 0) return n;
if (left >= str.length || str.length - left <= k) return 0;
if (dp[left][k] != -1) return dp[left][k];
int res = n;
List<int> cnt = List.filled(26, 0);
for (int j = left, freq = 0; j < str.length; j++) {
freq = max(freq, ++cnt[str[j].codeUnitAt(0) - 'a'.codeUnitAt(0)]);
res = min(
res,
1 +
getLen(freq) +
helper(
str,
j + 1,
k - (j - left + 1 - freq),
),
);
}
return dp[left][k] = res;
}
int getLengthOfOptimalCompression(String s, int k) {
dp = List.filled(n, 0).map((e) => List.filled(n, -1)).toList();
return helper(s, 0, k);
}
}
class E {
int getLength(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else if (n < 10) {
return 2;
} else if (n < 100) {
return 3;
} else {
return 4;
}
}
int recur(String s, String prevChar, int prevCharCount, int k, int index,
Map<String, int> memo) {
if (index == s.length) {
return 0;
}
String key = prevChar +
"," +
String.fromCharCode(prevCharCount) +
"," +
String.fromCharCode(k) +
"," +
String.fromCharCode(index);
int? keyVal = memo[key];
if (keyVal != null) {
return keyVal;
}
int ch = s.codeUnitAt(index);
int count = 1;
int nextIndex = index + 1;
for (int i = index + 1; i < s.length; i++) {
if (s.codeUnitAt(i) == ch) {
count++;
nextIndex = i + 1;
} else {
nextIndex = i;
break;
}
}
int totalCount = count;
int prevCountRepresentation = 0;
//if prev char is equal to current char that means we have removed middle element
//So we have to subtract the previous representation length and add the new encoding
//representation length
if (ch == prevChar) {
totalCount += prevCharCount;
prevCountRepresentation = getLength(prevCharCount);
}
int representationLength = getLength(totalCount);
int ans = representationLength +
recur(s, String.fromCharCode(ch), totalCount, k, nextIndex, memo) -
prevCountRepresentation;
if (k > 0) {
for (int i = 1; i <= k && i <= count; i++) {
int currentCount = totalCount - i;
int length = getLength(currentCount);
//checking if we have to send current char and current char count or previous char
//and previous char count
int holder = length +
recur(
s,
currentCount == 0 ? prevChar : String.fromCharCode(ch),
currentCount == 0 ? prevCharCount : currentCount,
k - i,
nextIndex,
memo) -
prevCountRepresentation;
ans = min(ans, holder);
}
}
memo.forEach((keys, values) {
keys = key;
values = ans;
});
return ans;
}
int getLengthOfOptimalCompression(String s, int k) {
HashMap<String, int> memo = HashMap();
return recur(s, '\u0000', 0, k, 0, memo);
}
}
// int getLengthOfOptimalCompression(String s, int k) {
// }
class M {
int getLengthOfOptimalCompression(String s, int k) {
// dp[i][k] := length of optimal compression of s[i:] w/ at most k deletion
// dp.resize(s.length, List<int>(k + 1, kMax));
dp = List.filled(s.length, 0).map((e) => List.filled(k + 1, kMax)).toList();
return compression(s, 0, k);
}
int kMax = 101;
late List<List<int>> dp;
int compression(String s, int i, int k) {
if (k < 0) return kMax;
if (i == s.length || s.length - i <= k) return 0;
if (dp[i][k] != kMax) return dp[i][k];
int maxFreq = 0; // Max freq in s[i..j]
List<int> count = List.filled(128, 0);
// Make chars in s[i..j] be same
// Keep the char that has max freq in this range and remove other chars
for (int j = i; j < s.length; ++j) {
maxFreq = max(maxFreq, ++count[s.codeUnitAt(j)]);
dp[i][k] = min(
dp[i][k],
getLength(maxFreq) +
compression(s, j + 1, k - (j - i + 1 - maxFreq)));
}
return dp[i][k];
}
int getLength(int maxFreq) {
// The length to compress `maxFreq`
if (maxFreq == 1) return 1; // C
if (maxFreq < 10) return 2; // [1-9]c
if (maxFreq < 100) return 3; // [1-9][0-9]c
return 4; // [1-9][0-9][0-9]c
}
}