Minimize removals to remove another string as a subsequence of a given string | C # program for above approach ; Function to print the minimum number of character removals required to remove X as a subsequence from the string str ; Length of the string str ; Length of the string X ; Stores the dp states ; Fill first row of dp [ ] [ ] ; If X [ j ] matches with str [ 0 ] ; If str [ i ] is equal to X [ j ] ; Update state after removing str [ i [ ; Update state after keeping str [ i ] ; If str [ i ] is not equal to X [ j ] ; Print the minimum number of characters removals ; Driver code ; Input ; Function call to get minimum number of character removals
using System ; public class GFG { static void printMinimumRemovals ( string str , string X ) { int N = str . Length ; int M = X . Length ; int [ , ] dp = new int [ N , M ] ; for ( int j = 0 ; j < M ; j ++ ) { 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 ] == X [ j ] ) { dp [ i , j ] = dp [ i - 1 , j ] + 1 ; if ( j != 0 ) dp [ i , j ] = Math . Min ( dp [ i , j ] , dp [ i - 1 , j - 1 ] ) ; } else { dp [ i , j ] = dp [ i - 1 , j ] ; } } } Console . WriteLine ( dp [ N - 1 , M - 1 ] ) ; } public static void Main ( String [ ] args ) { string str = " btagd " ; string X = " bad " ; printMinimumRemovals ( str , X ) ; } }