Minimum operations required to convert all characters of a String to a given Character | Java program to implement the above approach ; Function to find the minimum number of operations required ; Maximum number of characters that can be changed in one operation ; If length of the String less than maximum number of characters that can be changed in an operation ; Set the last index as the index for the operation ; Otherwise ; If size of the String is equal to the maximum number of characters in an operation ; Find the number of operations required ; Find the starting position ; Print i - th index ; Shift to next index ; Otherwise ; Find the number of operations required ; If n % div exceeds k ; Print i - th index ; Shift to next index ; Driver Code
class GFG { static void countOperations ( int n , int k ) { int div = 2 * k + 1 ; if ( n / 2 <= k ) { System . out . print ( 1 + "NEW_LINE"); if ( n > k ) System . out . print ( k + 1 ) ; else System . out . print ( n ) ; } else { if ( n % div == 0 ) { int oprn = n / div ; System . out . print ( oprn + "NEW_LINE"); int pos = k + 1 ; System . out . print ( pos + " ▁ " ) ; for ( int i = 1 ; i <= oprn ; i ++ ) { System . out . print ( pos + " ▁ " ) ; pos += div ; } } else { int oprn = n / div + 1 ; System . out . print ( oprn + "NEW_LINE"); int pos = n % div ; if ( n % div > k ) pos -= k ; for ( int i = 1 ; i <= oprn ; i ++ ) { System . out . print ( pos + " ▁ " ) ; pos += div ; } } } } public static void main ( String [ ] args ) { String str = " edfreqwsazxet " ; char ch = ' $ ' ; int n = str . length ( ) ; int k = 4 ; countOperations ( n , k ) ; } }