Minimum number of Appends of X or Y characters from the end to the front required to obtain given string | Java 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
import java . io . * ; import java . util . * ; class GFG { static void minimumOperations ( String orig_str , int m , int n ) { String orig = orig_str ; int turn = 1 ; int j = 1 ; for ( int i = 0 ; i < orig_str . length ( ) ; i ++ ) { String 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 . equals ( orig_str ) ) { turn = turn + 1 ; String 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 . equals ( orig_str ) ) { break ; } turn = turn + 1 ; } System . out . println ( turn ) ; } public static void main ( String [ ] args ) { String S = " GeeksforGeeks " ; int X = 5 , Y = 3 ; minimumOperations ( S , X , Y ) ; } }