Minimum increments by 1 or K required to convert a string into another given string | CPP program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countOperations ( string X , string Y , int K ) { int count = 0 ; for ( int i = 0 ; i < X . length ( ) ; i ++ ) { int c = 0 ; if ( X [ i ] == Y [ i ] ) continue ; else if ( X [ i ] < Y [ i ] ) { if ( ( Y [ i ] - X [ i ] ) >= K ) { c = ( Y [ i ] - X [ i ] ) / K ; } c += ( Y [ i ] - X [ i ] ) % K ; } else { int t = 90 - X [ i ] ; t += Y [ i ] - 65 + 1 ; if ( t >= K ) c = t / K ; c += ( t % K ) ; } count += c ; } cout << count << endl ; } int main ( ) { string X = " ABCT " , Y = " PBDI " ; int K = 6 ; countOperations ( X , Y , K ) ; }