Minimize length of a string by removing occurrences of another string from it as a substring | 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 ; Prthe final answer ; Driver Code ; Given string S & T ; Length of string S ; Length of string T ; Prints the count of operations required
def minLength ( S , T , N , M ) : NEW_LINE INDENT temp = " " ; NEW_LINE subtract = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp += S [ i ] ; NEW_LINE if ( len ( temp ) >= M ) : NEW_LINE INDENT if ( T == ( temp [ len ( temp ) - M : len ( temp ) ] ) ) : NEW_LINE INDENT subtract += M ; NEW_LINE cnt = 0 ; NEW_LINE while ( cnt != M ) : NEW_LINE INDENT temp = temp [ 0 : len ( temp ) - 1 ] ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( ( N - subtract ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " aabcbcbd " ; NEW_LINE T = " abc " ; NEW_LINE N = len ( S ) ; NEW_LINE M = len ( T ) ; NEW_LINE minLength ( S , T , N , M ) ; NEW_LINE DEDENT