Program to find minimum digits sum of deleted digits in Python

When working with two digit strings, we sometimes need to remove characters to make them identical while minimizing the sum of deleted digits. This problem can be solved using dynamic programming with a variation of the Longest Common Subsequence (LCS) algorithm.

Given two strings s and t containing only digits, we need to remove digits from both strings so that the remaining strings are identical, and the sum of deleted digits is minimized.

Example

If the input is s = "41272" and t = "172", we can remove "4" and "2" from the first string to get "172". The sum of deleted digits is 4 + 2 = 6.

Algorithm Approach

The solution uses a modified LCS algorithm that maximizes the sum of common digits instead of just counting them ?

  1. Calculate the total sum of all digits in both strings
  2. Find the maximum sum of digits that can remain (using modified LCS)
  3. Subtract the maximum remaining sum from the total sum

Implementation

class Solution:
    def lcs(self, a, b, m, n):
        # Create DP table to store maximum sum of common subsequence
        table = [[0 for i in range(n + 1)] for j in range(m + 1)]
        
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if a[i - 1] == b[j - 1]:
                    # If characters match, add twice the digit value
                    # (since it appears in both strings)
                    table[i][j] = table[i - 1][j - 1] + 2 * (ord(a[i - 1]) - 48)
                else:
                    # Take maximum from either excluding current char from a or b
                    table[i][j] = max(table[i - 1][j], table[i][j - 1])
        
        return table[m][n]
    
    def solve(self, a, b):
        m = len(a)
        n = len(b)
        
        # Calculate total sum of all digits in both strings
        total_sum = 0
        for i in range(m):
            total_sum += ord(a[i]) - 48
        for i in range(n):
            total_sum += ord(b[i]) - 48
        
        # Find maximum sum that can be preserved
        max_preserved = self.lcs(a, b, m, n)
        
        # Return the sum of digits that must be deleted
        return total_sum - max_preserved

# Test the solution
ob = Solution()
s = "41272"
t = "172"
print(ob.solve(s, t))
6

How It Works

The algorithm works by finding the maximum sum of digits that can be kept in both strings as a common subsequence. The LCS table stores the maximum sum of matching digits up to each position.

When characters match at position (i, j), we add twice the digit value because this digit contributes to both strings. When they don't match, we take the maximum value from either excluding the current character from string a or string b.

Step-by-Step Example

For s = "41272" and t = "172" ?

  1. Total sum = (4+1+2+7+2) + (1+7+2) = 16 + 10 = 26
  2. Maximum preserved sum = 2×(1+7+2) = 20 (common subsequence "172")
  3. Minimum deleted sum = 26 - 20 = 6

Conclusion

This solution efficiently finds the minimum sum of deleted digits using dynamic programming. The key insight is to maximize the sum of preserved common digits, then subtract from the total sum to get the minimum deletion cost.

Updated on: 2026-03-25T13:45:39+05:30

214 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements