Given two strings str and X of length N and M respectively, the task is to find the minimum characters required to be removed from the string str such that string str doesn’t contain the string X as a subsequence.
Examples:
Input: str = “btagd”, X = “bad”
Output: 1
Explanation:
String “btag” has does not contain “bad” as a subsequence. Therefore, only one removal is required.Input: str = “bbaaddd”, X = “bad”
Output: 2
Approach: This problem can be solved by Dynamic Programming. Follow the steps below to solve the problem:
- Traverse the string.
- Initialize a 2D array dp[N][M], where N is the length of string str and M is the length of string X.
- dp[i][j] represents the minimum number of characters removal required in the substring str[0, i] such that there is no occurrence of substring X[0, j] as a subsequence.
- The two transition states are :
- If str[i] is equal to X[j],
- Case 1 : Remove the character str[i]
- Case 2 : Maintain the character str[i], by ensuring that there is no occurrence of X[0, j-1] as a subsequence in str[0, i]
- Update dp[i][j] = min(dp[i − 1][j − 1], dp[i − 1][j] + 1)
- If str[i] is not equal to X[j], str[i] can be kept as it is.
- Update dp[i][j] = dp[i – 1][j]
- If str[i] is equal to X[j],
- Print the minimum removals i.e, dp[N – 1][M – 1]
Below is the implementation of the above approach:
C++
// C++ implementation of // the above approach #include <bits/stdc++.h> using namespace std; // Function to print the minimum number of // character removals required to remove X // as a subsequence from the string str void printMinimumRemovals(string str, string X) { // Length of the string str int N = str.size(); // Length of the string X int M = X.size(); // Stores the dp states int dp[N][M] = {}; // Fill first row of dp[][] for ( int j = 0; j < M; j++) { // If X[j] matches with str[0] if (str[0] == X[j]) { dp[0][j] = 1; } } for ( int i = 1; i < N; i++) { for ( int j = 0; j < M; j++) { // If str[i] is equal to X[j] if (str[i] == X[j]) { // Update state after removing str[i[ dp[i][j] = dp[i - 1][j] + 1; // Update state after keeping str[i] if (j != 0) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]); } // If str[i] is not equal to X[j] else { dp[i][j] = dp[i - 1][j]; } } } // Print the minimum number // of characters removals cout << dp[N - 1][M - 1]; } // Driver Code int main() { // Input string str = "btagd" ; string X = "bad" ; // Function call to get minimum // number of character removals printMinimumRemovals(str, X); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to print the minimum number of // character removals required to remove X // as a subsequence from the string str static void printMinimumRemovals(String str, String X) { // Length of the string str int N = str.length(); // Length of the string X int M = X.length(); // Stores the dp states int dp[][] = new int [N][M]; // Fill first row of dp[][] for ( int j = 0 ; j < M; j++) { // If X[j] matches with str[0] if (str.charAt( 0 ) == X.charAt(j)) { dp[ 0 ][j] = 1 ; } } for ( int i = 1 ; i < N; i++) { for ( int j = 0 ; j < M; j++) { // If str[i] is equal to X[j] if (str.charAt(i) == X.charAt(j)) { // Update state after removing str[i[ dp[i][j] = dp[i - 1 ][j] + 1 ; // Update state after keeping str[i] if (j != 0 ) dp[i][j] = Math.min( dp[i][j], dp[i - 1 ][j - 1 ]); } // If str[i] is not equal to X[j] else { dp[i][j] = dp[i - 1 ][j]; } } } // Print the minimum number // of characters removals System.out.println(dp[N - 1 ][M - 1 ]); } // Driver code public static void main(String[] args) { // Input String str = "btagd" ; String X = "bad" ; // Function call to get minimum // number of character removals printMinimumRemovals(str, X); } } // This code is contributed by Kingash. |
Python3
# Python 3 implementation of # the above approach # Function to print the minimum number of # character removals required to remove X # as a subsequence from the string str def printMinimumRemovals(s, X): # Length of the string str N = len (s) # Length of the string X M = len (X) # Stores the dp states dp = [[ 0 for x in range (M)] for y in range (N)] # Fill first row of dp[][] for j in range (M): # If X[j] matches with str[0] if (s[ 0 ] = = X[j]): dp[ 0 ][j] = 1 for i in range ( 1 , N): for j in range (M): # If s[i] is equal to X[j] if (s[i] = = X[j]): # Update state after removing str[i[ dp[i][j] = dp[i - 1 ][j] + 1 # Update state after keeping str[i] if (j ! = 0 ): dp[i][j] = min (dp[i][j], dp[i - 1 ][j - 1 ]) # If str[i] is not equal to X[j] else : dp[i][j] = dp[i - 1 ][j] # Print the minimum number # of characters removals print (dp[N - 1 ][M - 1 ]) # Driver Code if __name__ = = "__main__" : # Input s = "btagd" X = "bad" # Function call to get minimum # number of character removals printMinimumRemovals(s, X) # This code is contributed by ukasp. |
C#
// C# program for above approach using System; public class GFG { // Function to print the minimum number of // character removals required to remove X // as a subsequence from the string str static void printMinimumRemovals( string str, string X) { // Length of the string str int N = str.Length; // Length of the string X int M = X.Length; // Stores the dp states int [,] dp = new int [N, M]; // Fill first row of dp[][] for ( int j = 0; j < M; j++) { // If X[j] matches with str[0] if (str[0] == X[j]) { dp[0, j] = 1; } } for ( int i = 1; i < N; i++) { for ( int j = 0; j < M; j++) { // If str[i] is equal to X[j] if (str[i] == X[j]) { // Update state after removing str[i[ dp[i, j] = dp[i - 1, j] + 1; // Update state after keeping str[i] if (j != 0) dp[i, j] = Math.Min( dp[i, j], dp[i - 1, j - 1]); } // If str[i] is not equal to X[j] else { dp[i, j] = dp[i - 1, j]; } } } // Print the minimum number // of characters removals Console.WriteLine(dp[N - 1, M - 1]); } // Driver code public static void Main(String[] args) { // Input string str = "btagd" ; string X = "bad" ; // Function call to get minimum // number of character removals printMinimumRemovals(str, X); } } // This code is contributed by sanjoy_62. |
1
Time Complexity: O(N * M)
Auxiliary Space: O(N * M)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.