Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: String Compression II

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1531" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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.

Explanation

Here's a breakdown of the solution:

  • Dynamic Programming: We use dynamic programming to explore all possible deletions and keep track of the minimum run-length encoded length achievable for each prefix of the string with a given number of deletions.

  • State Definition: dp[i][j] represents the minimum length of the run-length encoded string for the first i characters of s after deleting at most j characters.

  • Transitions: We iterate through each character of the string and consider two possibilities: either we keep the character or we delete it. If we keep the character, we need to find the longest consecutive run of the same character ending at that position, and update the DP table based on whether we can extend an existing run or start a new one.

  • Base Case: dp[0][j] = 0 for all j, as an empty string has a run-length encoded length of 0 regardless of the number of allowed deletions.

  • Runtime & Storage Complexity:

    • Runtime Complexity: O(n2 * k), where n is the length of the string and k is the maximum number of deletions allowed.
    • Storage Complexity: O(n * k)

Code

    def get_len(count):
    if count == 1:
        return 0
    elif count < 10:
        return 1
    elif count < 100:
        return 2
    else:
        return 3

def solve():
    s = input()
    k = int(input())
    n = len(s)
    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):
            # Delete s[i-1]
            if j > 0:
                dp[i][j] = min(dp[i][j], dp[i - 1][j - 1])

            # Keep s[i-1]
            for l in range(i - 1, -1, -1):
                deletions = 0
                count = 0
                for m in range(l, i):
                    if s[m] == s[i - 1]:
                        count += 1
                    else:
                        deletions += 1

                if j >= deletions:
                    length = get_len(count) + 1 if count > 0 else 0

                    if l == 0:
                        dp[i][j] = min(dp[i][j], length)
                    else:
                        dp[i][j] = min(dp[i][j], dp[l][j - deletions] + length)

    ans = float('inf')
    for j in range(k + 1):
        ans = min(ans, dp[n][j])

    print(ans)
solve()

More from this blog

C

Chatmagic blog

2894 posts