LeetCode 1531 Solution

LeetCode 1531 Solution

1531. String Compression

#Hard #

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.

class Solution:
    def getLengthOfOptimalCompression(self, s: str, k: int) -> int:
        n = len(s)

        # Function to calculate the extra length added by encoding a run of characters
        def encoded_extra_length(run_length):
            if run_length == 0:
                return 0
            elif run_length == 1:
                return 1
            elif run_length < 10:
                return 2
            elif run_length < 100:
                return 3
            else:
                return 4

        # Initialize DP array with high values
        dp = [[float('inf')] * (k + 1) for _ in range(n + 1)]
        dp[0][0] = 0

        for i in range(1, n + 1):
            for j in range(k + 1):
                cnt, del_ = 0, 0
                # Iterate backwards to find all possible combinations of deletions and character runs
                for l in range(i, 0, -1):
                    if s[l - 1] == s[i - 1]:
                        cnt += 1
                    else:
                        del_ += 1
                        if del_ > j:
                            break
                    dp[i][j] = min(dp[i][j], dp[l - 1][j - del_] + encoded_extra_length(cnt))

                # Handling deletions
                if j > 0:
                    dp[i][j] = min(dp[i][j], dp[i - 1][j - 1])

        return dp[n][k]