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
function countOperations ( X , Y , K ) { var count = 0 ; for ( var i = 0 ; i < X . length ; i ++ ) { var c = 0 ; if ( X [ i ] === Y [ i ] ) continue ; else if ( X [ i ] . charCodeAt ( 0 ) < Y [ i ] . charCodeAt ( 0 ) ) { if ( Y [ i ] . charCodeAt ( 0 ) - X [ i ] . charCodeAt ( 0 ) >= K ) { c = ( Y [ i ] . charCodeAt ( 0 ) - X [ i ] . charCodeAt ( 0 ) ) / K ; } c += ( Y [ i ] . charCodeAt ( 0 ) - X [ i ] . charCodeAt ( 0 ) ) % K ; } else { var t = 90 - X [ i ] . charCodeAt ( 0 ) ; t += Y [ i ] . charCodeAt ( 0 ) - 65 + 1 ; if ( t >= K ) { c = parseInt ( t / K ) ; } c += parseInt ( t % K ) ; } count = parseInt ( count + c ) ; } document . write ( count + " " ) ; } var X = " " , Y = " " ; var K = 6 ; countOperations ( X , Y , K ) ;