Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions String Compression II.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// We define the state dp[i][k]: the minimum length for s.substring(0, i+1) with at most k deletion.
// For each char s[i], we can either keep it or delete it.
// If delete, dp[i][j]=dp[i-1][j-1].
// If keep, we delete at most j chars in s.substring(0, i+1) that are different from s[i].

class Solution {
public int getLengthOfOptimalCompression(String s, int k) {
// dp[i][k]: the minimum length for s[:i] with at most k deletion.
int n = s.length();
int[][] dp = new int[110][110];
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], concat the same, remove the different
if(s.charAt(l - 1) == s.charAt(i - 1)) cnt++;
else del++;
if(j - del >= 0)
dp[i][j] = Math.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] = Math.min(dp[i][j], dp[i-1][j-1]);
}
}
return dp[n][k];
}
}