text: Minimum number of Appends of X or Y characters from the end to the front required to obtain given string | C ++ program for the above approach ; 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 ; Driver Code ; Given string S ; Function Call
code: #include <iostream> NEW_LINE using namespace std ; int minimumOperations ( string orig_str , int m , int n ) { string orig = orig_str ; int turn = 1 ; int j = 1 ; for ( auto i : orig_str ) { string m_cut = orig_str . substr ( orig_str . length ( ) - m ) ; orig_str . erase ( orig_str . length ( ) - m ) ; orig_str = m_cut + orig_str ; j = j + 1 ; if ( orig != orig_str ) { turn = turn + 1 ; string n_cut = orig_str . substr ( orig_str . length ( ) - n ) ; orig_str . erase ( orig_str . length ( ) - n ) ; orig_str = n_cut + orig_str ; j = j + 1 ; } if ( orig == orig_str ) { break ; } turn = turn + 1 ; } cout << turn ; } int main ( ) { string S = " GeeksforGeeks " ; int X = 5 , Y = 3 ; minimumOperations ( S , X , Y ) ; return 0 ; }