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 ; Print the final answer ; Given string S & T ; Length of string S ; Length of string T ; Prints the count of operations required
function minLength ( S , T , N , M ) { let temp = " " ; let subtract = 0 ; for ( let i = 0 ; i < N ; ++ i ) { temp += S [ i ] ; if ( temp . length >= M ) { if ( T == temp . substr ( temp . length - M , temp . length ) ) { subtract += M ; let cnt = 0 ; while ( cnt != M ) { temp = temp . substr ( 0 , temp . length - 1 ) ; ++ cnt ; } } } } document . write ( ( N - subtract ) ) ; } let S = " " , T = " " ; let N = S . length ; let M = T . length ; minLength ( S , T , N , M ) ;