Open In App

Minimum increments by 1 or K required to convert a string into another given string

Last Updated : 28 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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>


Output: 

11

 

Time Complexity: O(N)
Auxiliary Space: O(1) because it is using constant space



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads