返回介绍

solution / 1500-1599 / 1531.String Compression II / README_EN

发布于 2024-06-17 01:03:18 字数 4589 浏览 0 评论 0 收藏 0

1531. String Compression II

中文文档

Description

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.

Solutions

Solution 1

class Solution {
  public int getLengthOfOptimalCompression(String s, int k) {
    // dp[i][k] := the length of the optimal compression of s[i..n) with at most
    // k deletion
    dp = new int[s.length()][k + 1];
    Arrays.stream(dp).forEach(A -> Arrays.fill(A, K_MAX));
    return compression(s, 0, k);
  }

  private static final int K_MAX = 101;
  private int[][] dp;

  private int compression(final String s, int i, int k) {
    if (k < 0) {
      return K_MAX;
    }
    if (i == s.length() || s.length() - i <= k) {
      return 0;
    }
    if (dp[i][k] != K_MAX) {
      return dp[i][k];
    }
    int maxFreq = 0;
    int[] count = new int[128];
    // Make letters in s[i..j] be the same.
    // Keep the letter that has the maximum frequency in this range and remove
    // the other letters.
    for (int j = i; j < s.length(); ++j) {
      maxFreq = Math.max(maxFreq, ++count[s.charAt(j)]);
      dp[i][k] = Math.min(
        dp[i][k], getLength(maxFreq) + compression(s, j + 1, k - (j - i + 1 - maxFreq)));
    }
    return dp[i][k];
  }

  // Returns the length to compress `maxFreq`.
  private int getLength(int 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
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文