VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimium-cost-to-delete-characters-from-string-a-to-remove-any-subsequence-as-string-b/

⇱ Minimum cost to delete characters from String A to remove any subsequence as String B - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum cost to delete characters from String A to remove any subsequence as String B

Last Updated : 23 Jul, 2025

Given two strings A and B of size N and M respectively, where B is a sub-sequence of A and an array arr[] of size N, where arr[i] is the cost to delete ith character from string A. The task is to find the minimum cost to delete characters from A such that after deletion no subsequence of A is the same as B.

Examples:

Input: A = "abccd", B = "ccd", arr[] = {1, 2, 3, 4, 5}
Output: 3
Explanation: If we remove either 'd' or a single 'c' from A then it will not be possible to construct a sub-sequence equals to B. 
Among these the cost to remove the 'c' at index 2 is minimum that is 3. So the answer is 3.

Input: A = "abccdabccdabccd", B = "bccd", arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
Output: 21
Explanation: If we remove the three 'b's with cost 2, 7 and 5, then A becomes "accdaccdaccd". 
Now there is no way to construct a sub-sequence = "bccd" 

Approach: The naive approach to solve the problem is to use recursion and to find a common subsequence between A and B as per the following idea:

If the character of A and B matches there will be 2 options: either remove that character from A or keep that character and remove other characters.

Follow the steps mentioned below to implement the idea:

  • Recursively traverse through string A using pointer i and keep a pointer j to point to B.
  • In each recursive function:
    • If the end of the string is reached return 0 if any character is removed (checked by a counter of deleted elements) otherwise return high positive value.
    • If A[i] = B[j] there are two choices:
      • Remove A[i] and add cost arr[i] to answer and recursively call for the next elements.
      • Keep A[i] and move forward.
    • Else keep moving forward until A[i] matches B[i].
    • Return the minimum cost among the above two cases from each recursive call.
  • Return the minimum cost as answer.

Time Complexity: O(2M)
Auxiliary Space: O(1)

Efficient Approach: The time of the above approach can be further optimized using Dynamic Programming as per the following idea:

Use a 3D dp[][][] array to store the minimum cost until a given position in A and B and for removing at least one character. dp[i][j][] stores minimum cost to delete characters till ith index of A and jth index of B where either at least one character is deleted or not. 
Hence the third dimension only has two value either 1 (at least one is deleted) or 0 (none is deleted)

Below is the implementation of the above approach.


Output
21

Time Complexity: O(N*M)
Auxiliary Space: O(N*M)

Iterative approach : Using DP Tabulation method 

The approach to solve this problem is same but DP tabulation(bottom-up) method is better then Dp + memorization(top-down) because memorization method needs extra stack space of recursion calls.

Steps to solve this problem :

  • Create a 3D dynamic programming table dp[n+1][m+1][m+1], where n is the length of string 'a', m is the length of string 'b', and the third dimension represents the number of characters to be removed.
  • Initialize base cases:
    a. If either i or j equals 0, then if k equals 0, set dp[i][j][k] to INT_MAX, else set it to 0.
    b. If i and j are greater than 0, set dp[i][j][k] to INT_MAX.
  • Tabulate the subproblems:
    a. Iterate over i from 1 to n, j from 1 to m, and k from 1 to removed.
    b. If a[i-1] equals b[j-1], set dp[i][j][k] to min(c[i-1] + dp[i-1][j][k], dp[i-1][j-1][k-1]).
    c. Else, set dp[i][j][k] to dp[i-1][j][k].
  • Find the minimum cost of removing m characters from a:
    a. Initialize ans as INT_MAX.
    b. Iterate over k from 1 to removed, and set ans to min(ans, dp[n][m][k]).
  • Return ans if it is less than INT_MAX, else return -1.

Implementation :

Output:

21

Time complexity: O(n*m^2)

Auxiliary Space: O(n*m^2)

Comment