Minimum increments by 1 or K required to convert a string into another given string | Function to count minimum increments by 1 or K required to convert X to Y ; Traverse the string X ; Case 1 ; Case 2 ; Add the difference / K to the count ; Add the difference % K to the count ; Case 3 ; Add the difference / K to the count ; Add the difference % K to the count ; Print the answer ; Driver Code
def countOperations ( X , Y , K ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( X ) ) : NEW_LINE INDENT c = 0 NEW_LINE if ( X [ i ] == Y [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( X [ i ] < Y [ i ] ) : NEW_LINE INDENT if ( ( ord ( Y [ i ] ) - ord ( X [ i ] ) ) >= K ) : NEW_LINE INDENT c = ( ord ( Y [ i ] ) - ord ( X [ i ] ) ) // K NEW_LINE DEDENT c += ( ord ( Y [ i ] ) - ord ( X [ i ] ) ) % K NEW_LINE DEDENT else : NEW_LINE INDENT t = 90 - ord ( X [ i ] ) NEW_LINE t += ord ( Y [ i ] ) - 65 + 1 NEW_LINE if ( t >= K ) : NEW_LINE INDENT c = t // K NEW_LINE DEDENT c += ( t % K ) NEW_LINE DEDENT count += c NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT X = " ABCT " NEW_LINE Y = " PBDI " NEW_LINE K = 6 NEW_LINE countOperations ( X , Y , K ) NEW_LINE