Minimium cost to delete characters from String A to remove any subsequence as String B
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
Explaination: 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.
C++
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Array for memoizationint dp[101][101][2]; // Recursive function to calculate// the minimum cost using dynamic programmingint minCostUtil(string& a, int n, string& b, int m, vector<int>& c, int removed){ // Base case reached the end of string if (n == 0 || m == 0) { // Removed 0 characters // return high (+)ve value if (removed == 0) return 99999; return 0; } // Return pre-calculated value if (dp[n][m][removed > 0 ? 1 : 0] != -1) return dp[n][m][removed > 0 ? 1 : 0]; // If characters match return the minimum of // 1. Removing the character from A and // adding the cost // 2. Moving forward to remove some other // character and decrease the counter as // this character will not be removed. if (a[n - 1] == b[m - 1]) { dp[n][m][removed > 0 ? 1 : 0] = min(c[n - 1] + minCostUtil(a, n - 1, b, m, c, removed), minCostUtil(a, n - 1, b, m - 1, c, removed - 1)); return dp[n][m][removed > 0 ? 1 : 0]; } // If no match then move through string // A and try removing some other // character which matches, i.e can be // part of the subsequence that is equal to B else return dp[n][m][removed > 0 ? 1 : 0] = minCostUtil(a, n - 1, b, m, c, removed);} // Function to calculate minimum costint minCost(string& a, string& b, vector<int>& c){ memset(dp, -1, sizeof(dp)); return minCostUtil(a, a.size(), b, b.size(), c, b.size());} // Driver codeint main(){ string A = "abccdabccdabccd"; string B = "bccd"; vector<int> arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; cout << minCost(A, B, arr); return 0;} |
21
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)

