Given two strings X and Y both consisting of N uppercase alphabets and an integer K, the task is to find the minimum count of cyclic increments of any character in the string X by 1 or K required to convert the string X into string Y.
Cyclic Increments: Incrementing character āZā by 1 or K starts from the character āAā.
Examples:
Input: X = āABCTā, Y = āPBDIā, K = 13
Output: 7
Explanation:Ā
Convert the character A to P. Increments required are 13 (A to N), 1 (N to O), 1 (O to P). Therefore, the total count is 3.
Character B remains unchanged. Therefore, the total count remains 3.
Convert the character C to D. Increments required are 1 (C to D). Therefore, the total count is 4.
Convert the character T to I. Increments required is of 13 (T to G), 1 (G to H), 1 (H to I). Therefore, the total count is 7.Input: X = āABCDā, Y = āABCDā, K = 6
Output: 0
Approach: Follow the below steps to solve this problem:
- Initialize a variable, say count, to store the minimum increment required to convert string X into string Y.
- Traverse the string X and for every character in string X, there are three following cases:
- When characters in both the strings X and Y are the same then continue.
- When characters in the string Y is greater than X string then add the value (Y[i] ā X[i])/K and (Y[i] ā X[i]) modulo K to the count.
- When characters in string X is greater than string Y then subtract the character X[i] from 90 and subtract 64 from Y[i] to calculate the cyclic difference and then add the difference modulo K and difference/K to the count.
- After completing the above steps, print count as the minimum number of increments required.
Below is the implementation of the above approach:
C++
// CPP program for the above approach #include<bits/stdc++.h> using namespace std; Ā
// Function to count minimum increments // by 1 or K required to convert X to Y void countOperations( Ā Ā string X, string Y, int K) { Ā
Ā Ā int count = 0; Ā
Ā Ā // Traverse the string X Ā Ā for ( int i = 0; i < X.length(); i++) Ā Ā { Ā
Ā Ā Ā Ā int c = 0; Ā
Ā Ā Ā Ā // Case 1 Ā Ā Ā Ā if (X[i] == Y[i]) Ā Ā Ā Ā Ā Ā continue ; Ā
Ā Ā Ā Ā // Case 2 Ā Ā Ā Ā else if (X[i] < Y[i]) Ā Ā Ā Ā { Ā
Ā Ā Ā Ā Ā Ā // Add the difference/K to Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā if ((Y[i] - X[i]) >= K) { Ā Ā Ā Ā Ā Ā Ā Ā c = (Y[i] - X[i]) / K; Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā // Add the difference%K to Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā c += (Y[i] - X[i]) % K; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Case 3 Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā int t = 90 - X[i]; Ā Ā Ā Ā Ā Ā t += Y[i] - 65 + 1; Ā
Ā Ā Ā Ā Ā Ā // Add the difference/K to Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā if (t >= K) Ā Ā Ā Ā Ā Ā Ā Ā c = t / K; Ā
Ā Ā Ā Ā Ā Ā // Add the difference%K to Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā c += (t % K); Ā Ā Ā Ā } Ā Ā Ā Ā count += c; Ā Ā } Ā
Ā Ā // Print the answer Ā Ā cout << count << endl; } Ā
// Driver Code int main() { Ā Ā string X = "ABCT" , Y = "PBDI" ; Ā Ā int K = 6; Ā Ā countOperations(X, Y, K); } Ā
// This code is contributed by ipg2016107. |
Java
// Java program for the above approach Ā
import java.io.*; Ā
class GFG { Ā
Ā Ā Ā Ā // Function to count minimum increments Ā Ā Ā Ā // by 1 or K required to convert X to Y Ā Ā Ā Ā public static void countOperations( Ā Ā Ā Ā Ā Ā Ā Ā String X, String Y, int K) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā // Convert String to character array Ā Ā Ā Ā Ā Ā Ā Ā char ch1[] = X.toCharArray(); Ā Ā Ā Ā Ā Ā Ā Ā char ch2[] = Y.toCharArray(); Ā Ā Ā Ā Ā Ā Ā Ā int count = 0 ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the string X Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0 ; i < X.length(); i++) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int c = 0 ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Case 1 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (ch1[i] == ch2[i]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Case 2 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (ch1[i] < ch2[i]) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference/K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((( int )ch2[i] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā - ( int )ch1[i]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā >= K) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c = (( int )ch2[i] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā - ( int )ch1[i]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā / K; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference%K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c += (( int )ch2[i] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā - ( int )ch1[i]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % K; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Case 3 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int t = 90 - ( int )ch1[i]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā t += ( int )ch2[i] - 65 + 1 ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference/K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (t >= K) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c = t / K; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference%K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c += (t % K); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count += c; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Print the answer Ā Ā Ā Ā Ā Ā Ā Ā System.out.print(count); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Driver Code Ā Ā Ā Ā public static void main(String[] args) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā String X = "ABCT" , Y = "PBDI" ; Ā Ā Ā Ā Ā Ā Ā Ā int K = 6 ; Ā Ā Ā Ā Ā Ā Ā Ā countOperations(X, Y, K); Ā Ā Ā Ā } } |
Python3
# Python program for the above approach Ā
# Function to count minimum increments # by 1 or K required to convert X to Y def countOperations(X, Y, K): Ā Ā Ā Ā count = 0 Ā Ā Ā Ā Ā Ā Ā Ā Ā # Traverse the string X Ā Ā Ā Ā for i in range ( len (X)): Ā Ā Ā Ā Ā Ā Ā Ā c = 0 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Case 1 Ā Ā Ā Ā Ā Ā Ā Ā if (X[i] = = Y[i]): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Case 2 Ā Ā Ā Ā Ā Ā Ā Ā elif (X[i] < Y[i]): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Add the difference/K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (( ord (Y[i]) - ord (X[i])) > = K): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c = ( ord (Y[i]) - ord (X[i])) / / K Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Add the difference%K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c + = ( ord (Y[i]) - ord (X[i])) % K Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Case 3 Ā Ā Ā Ā Ā Ā Ā Ā else : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā t = 90 - ord (X[i]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā t + = ord (Y[i]) - 65 + 1 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Add the difference/K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (t > = K): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c = t / / K Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Add the difference%K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c + = (t % K) Ā Ā Ā Ā Ā Ā Ā Ā count + = c Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Print the answer Ā Ā Ā Ā print (count) Ā
# Driver Code X = "ABCT" Y = "PBDI" K = 6 countOperations(X, Y, K) Ā
# This code is contributed by aditya7409. |
C#
// C# program for the above approach using System; class GFG { Ā
Ā Ā // Function to count minimum increments Ā Ā // by 1 or K required to convert X to Y Ā Ā static void countOperations( Ā Ā Ā Ā string X, string Y, int K) Ā Ā { Ā Ā Ā Ā // Convert String to character array Ā Ā Ā Ā char [] ch1 = X.ToCharArray(); Ā Ā Ā Ā char [] ch2 = Y.ToCharArray(); Ā Ā Ā Ā int count = 0; Ā
Ā Ā Ā Ā // Traverse the string X Ā Ā Ā Ā for ( int i = 0; i < X.Length; i++) { Ā
Ā Ā Ā Ā Ā Ā int c = 0; Ā
Ā Ā Ā Ā Ā Ā // Case 1 Ā Ā Ā Ā Ā Ā if (ch1[i] == ch2[i]) Ā Ā Ā Ā Ā Ā Ā Ā continue ; Ā
Ā Ā Ā Ā Ā Ā // Case 2 Ā Ā Ā Ā Ā Ā else if (ch1[i] < ch2[i]) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference/K to Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā if ((( int )ch2[i] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā - ( int )ch1[i]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā >= K) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c = (( int )ch2[i] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā - ( int )ch1[i]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā / K; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference%K to Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā c += (( int )ch2[i] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā - ( int )ch1[i]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % K; Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā // Case 3 Ā Ā Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā int t = 90 - ( int )ch1[i]; Ā Ā Ā Ā Ā Ā Ā Ā t += ( int )ch2[i] - 65 + 1; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference/K to Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā if (t >= K) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c = t / K; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference%K to Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā c += (t % K); Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā count += c; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Print the answer Ā Ā Ā Ā Console.WriteLine(count); Ā Ā } Ā
Ā Ā // Driver code Ā Ā static void Main() Ā Ā { Ā Ā Ā Ā string X = "ABCT" , Y = "PBDI" ; Ā Ā Ā Ā int K = 6; Ā Ā Ā Ā countOperations(X, Y, K); Ā Ā } } Ā
// This code is contributed by susmitakundugoaldanga. |
Javascript
<script> Ā Ā Ā Ā Ā Ā // JavaScript program for the above approach Ā Ā Ā Ā Ā Ā // Function to count minimum increments Ā Ā Ā Ā Ā Ā // by 1 or K required to convert X to Y Ā Ā Ā Ā Ā Ā function countOperations(X, Y, K) Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā var count = 0; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the string X Ā Ā Ā Ā Ā Ā Ā Ā for ( var i = 0; i < X.length; i++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā var c = 0; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Case 1 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (X[i] === Y[i]) continue ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Case 2 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (X[i].charCodeAt(0) < Y[i].charCodeAt(0)) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference/K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (Y[i].charCodeAt(0) - X[i].charCodeAt(0) >= K) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c = (Y[i].charCodeAt(0) - X[i].charCodeAt(0)) / K; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference%K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c += (Y[i].charCodeAt(0) - X[i].charCodeAt(0)) % K; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Case 3 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā var t = 90 - X[i].charCodeAt(0); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā t += Y[i].charCodeAt(0) - 65 + 1; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference/K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (t >= K) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c = parseInt(t / K); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Add the difference%K to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the count Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā c += parseInt(t % K); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count = parseInt(count + c); Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Print the answer Ā Ā Ā Ā Ā Ā Ā Ā document.write(count + "<br>" ); Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā // Driver Code Ā Ā Ā Ā Ā Ā var X = "ABCT" , Ā Ā Ā Ā Ā Ā Ā Ā Y = "PBDI" ; Ā Ā Ā Ā Ā Ā var K = 6; Ā Ā Ā Ā Ā Ā countOperations(X, Y, K); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // This code is contributed by rdtank. Ā Ā Ā Ā </script> |
11
Ā
Time Complexity: O(N)
Auxiliary Space: O(1) because it is using constant space
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!