Minimize the count of characters to be added or removed to make String repetition of same substring | Function to find the minimum of the three numbers ; Function to find the minimum number operations required to convert string str1 to str2 using the operations ; Stores the results of subproblems ; Fill dp [ ] [ ] in bottom up manner ; If str1 is empty , then insert all characters of string str2 ; Minimum operations is j ; If str2 is empty , then remove all characters of string str2 ; Minimum operations is i ; If the last characters are same , then ignore last character ; If the last character is different , then find the minimum ; Perform one of the insert , remove and the replace ; Return the minimum number of steps required ; Function to find the minimum number of steps to modify the string such that first half and second half becomes the same ; Stores the minimum number of operations required ; Traverse the given string S ; Find the minimum operations ; Update the ans ; Print the result ; Driver Code
function getMin ( x , y , z ) { return Math . min ( Math . min ( x , y ) , z ) ; } function editDistance ( str1 , str2 , m , n ) { let dp = new Array ( m + 1 ) . fill ( new Array ( n + 1 ) ) ; for ( let i = 0 ; i <= m ; i ++ ) { for ( let j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i ] [ j ] = j ; else if ( j == 0 ) dp [ i ] [ j ] = i ; else if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else { dp [ i ] [ j ] = 1 + getMin ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) ; } } } return dp [ m ] [ n ] ; } function minimumSteps ( S , N ) { let ans = Number . MAX_VALUE ; for ( let i = 1 ; i < N ; i ++ ) { let S1 = S . substring ( 0 , i ) ; let S2 = S . substring ( i ) ; let count = editDistance ( S1 , S2 , S1 . length , S2 . length ) ; ans = Math . min ( ans , count ) ; } document . write ( ans - 1 ) ; } let S = " " ; let N = S . length ; minimumSteps ( S , N ) ;