Minimize length of a string by removing occurrences of another string from it as a substring | C # program for the above approach ; Function to minimize length of string S after removing all occurrences of string T as substring ; Count of characters required to be removed ; Iterate over the string ; Insert the current character to temp ; Check if the last M characters forms t or not ; Getting the last M characters . If they are equal to t then remove the last M characters from the temp string ; Incrementing subtract by M ; Removing last M characters from the string ; Print the readonly answer ; Driver Code ; Given string S & T ; Length of string S ; Length of string T ; Prints the count of operations required
using System ; class GFG { static void minLength ( String S , String T , int N , int M ) { String temp = " " ; int subtract = 0 ; for ( int i = 0 ; i < N ; ++ i ) { temp += S [ i ] ; if ( temp . Length >= M ) { if ( T . Equals ( temp . Substring ( temp . Length - M , M ) ) ) { subtract += M ; int cnt = 0 ; while ( cnt != M ) { temp = temp . Substring ( 0 , temp . Length - 1 ) ; ++ cnt ; } } } } Console . WriteLine ( ( N - subtract ) ) ; } public static void Main ( String [ ] args ) { String S = " aabcbcbd " , T = " abc " ; int N = S . Length ; int M = T . Length ; minLength ( S , T , N , M ) ; } }