Minimum number of Appends of X or Y characters from the end to the front required to obtain given string | Function to find the minimum operations required to get the given string after appending m or n characters from the end to the front of the string in each operation ; Store the original string ; Stores the count of operations ; Traverse the string ; Cut m letters from end ; Add cut m letters to beginning ; Update j ; Check if strings are the same ; Cut n letters from end ; Add cut n letters to beginning ; Update j ; Check if strings are the same ; Update the turn ; Given string S ; Function Call
function minimumOperations ( orig_str , m , n ) { let orig = orig_str ; let turn = 1 ; let j = 1 ; for ( let i = 0 ; i < orig_str . length ; i ++ ) { let m_cut = orig_str . substring ( orig_str . length - m ) ; orig_str = orig_str . substring ( 0 , orig_str . length - m ) ; orig_str = m_cut + orig_str ; j = j + 1 ; if ( orig != orig_str ) { turn = turn + 1 ; let n_cut = orig_str . substring ( orig_str . length - n ) ; orig_str = orig_str . substring ( 0 , orig_str . length - n ) ; orig_str = n_cut + orig_str ; j = j + 1 ; } if ( orig == orig_str ) { break ; } turn = turn + 1 ; } document . write ( turn ) ; } let S = " " ; let X = 5 , Y = 3 ; minimumOperations ( S , X , Y ) ;