Minimum operations required to convert all characters of a String to a given Character | Function to find the minimum number of operations required ; Maximum number of characters that can be changed in one operation ; If length of the 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 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
def countOperations ( n , k ) : NEW_LINE INDENT div = 2 * k + 1 NEW_LINE if ( n // 2 <= k ) : NEW_LINE INDENT print ( 1 ) NEW_LINE if ( n > k ) : NEW_LINE INDENT print ( k + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( n % div == 0 ) : NEW_LINE INDENT oprn = n // div NEW_LINE print ( oprn ) NEW_LINE pos = k + 1 NEW_LINE print ( pos , end = " ▁ " ) NEW_LINE for i in range ( 1 , oprn + 1 ) : NEW_LINE INDENT print ( pos , end = " ▁ " ) NEW_LINE pos += div NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT oprn = n // div + 1 NEW_LINE print ( oprn ) NEW_LINE pos = n % div NEW_LINE if ( n % div > k ) : NEW_LINE INDENT pos -= k NEW_LINE DEDENT for i in range ( 1 , oprn + 1 ) : NEW_LINE INDENT print ( pos , end = " ▁ " ) NEW_LINE pos += div NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " edfreqwsazxet " NEW_LINE ch = ' $ ' NEW_LINE n = len ( str ) NEW_LINE k = 4 NEW_LINE countOperations ( n , k ) NEW_LINE DEDENT