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
def minimumOperations ( orig_str , m , n ) : NEW_LINE INDENT orig = orig_str NEW_LINE turn = 1 NEW_LINE j = 1 NEW_LINE for i in orig_str : NEW_LINE INDENT m_cut = orig_str [ - m : ] NEW_LINE orig_str = orig_str . replace ( ' ▁ ' , ' ' ) [ : - m ] NEW_LINE orig_str = m_cut + orig_str NEW_LINE j = j + 1 NEW_LINE if orig != orig_str : NEW_LINE INDENT turn = turn + 1 NEW_LINE n_cut = orig_str [ - n : ] NEW_LINE orig_str = orig_str . replace ( ' ▁ ' , ' ' ) [ : - n ] NEW_LINE orig_str = n_cut + orig_str NEW_LINE j = j + 1 NEW_LINE DEDENT if orig == orig_str : NEW_LINE INDENT break NEW_LINE DEDENT turn = turn + 1 NEW_LINE DEDENT print ( turn ) NEW_LINE DEDENT S = " GeeksforGeeks " NEW_LINE X = 5 NEW_LINE Y = 3 NEW_LINE minimumOperations ( S , X , Y ) NEW_LINE