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


We have given two strings and need to convert one string to another by click increments, and we can increment characters by either 1 or k in the single operation.

To solve the problem, we need to make all characters of the first string the same as a second character by performing the cyclic increment operations. If the character at the same index in both strings is the same, we don’t need to perform any increment operation.

Problem statement – We have given two strings named first and second containing the uppercase alphabetical characters. The length of both strings is N. Also, we have given the positive integer K, representing we can increment character by either 1 or k in a single operation. We need to find the total number of cyclic increments to convert the string from first to second.

Sample examples

Input– first = "ACSQ", second = "BCOS", k = 7

Output– 7

Explanation

  • To convert A to B, we need 1 operation of 1 increment.

  • To convert C to C, we need to perform 0 operations.

  • To convert S to O, we need a total of 22 cyclic increments. We can perform (k = 3) operations of 7 increments and 1 operation of 1 increment. Total 4 operations we need.

  • To convert Q to S, we need 2 increments. So total operations required is 2 as we need to convert Q -> R and R -> S.

  • Total operations required is 1 + 0 + 4 + 2 = 7.

Input– first = "ACSQ", second = "BCOS", K = 9

Output– 0

Explanation– We don’t need to perform any increment operations as all characters of both strings are the same.

Input– first = "AB", second = "FG", K = 6

Output– 6

Explanation

  • To convert A to F, we need 5 increments. So, we can perform 1 operation of 3 increments and 2 operations of 1 increment. We need a total of 3 operations.

  • To convert B to G, we need 5 increments. So, we need to perform 3 operations.

  • We need a total of 6 operations.

Approach 1

In this approach, we will traverse the string. After that, we will find the cyclic differences between the characters. We can get the total number of increments by K by dividing the difference with K and the total number of increments by 1 by performing the modulo operation of different with K.

Algorithm

  • Initialize the ‘total’ variable with zero to store the number of operations.

  • Traverse the string using the loop.

  • If the first and second strings have the same character at ith index, use the ‘continue’ keyword to move to the next iteration, as we don’t need to perform any operations.

  • If the character of the first string is smaller than that of the second string at ith index, follow the steps below.

    • If the second[i] - first[i] is greater than K, add (second[i] - first[i]) / K to the ‘cnt’ variable, which is zero initially. It gives us a total number of operations we can perform by increasing the characters by K.

    • Add (second[i] - first[i]) % K to the ‘cnt’, representing the total number of increments required by 1.

  • If the character of the first string is greater than that of the second string at the ith index, follow the below steps.

    • Get the difference (first[i] - second[i]), subtract it from 26, and store it to the ‘temp’ variable.

    • If the temp is greater than K, add temp / K to the ‘cnt’ variable.

    • Add temp % K to the ‘cnt’ variable.

  • Add the ‘cnt’ value to ‘total’ when one loop iteration completes.

  • Return the ‘total’ value.

Example

#include <bits/stdc++.h>
using namespace std;
// function to find the total minimum operations required to convert the string first to second with the given condition
int totalOperations(string first, string second, int K){
   // to store the count of operations
   int total = 0;
   // Traverse the string first
   for (int i = 0; i < first.length(); i++){
      // to store the total operations required to convert the current character of first to second
      int cnt = 0;
      // If the characters are the same, then continue
      if (first[i] == second[i])
          continue;
      // If the character of first is less than the character of second
      else if (first[i] < second[i]){
          // If the difference is greater than K
          if ((second[i] - first[i]) >= K){
              // Add the difference/K to cnt
              cnt = (second[i] - first[i]) / K;
          }
          // Add the difference%K to the cnt
          cnt += (second[i] - first[i]) % K;
      }
      // If the character of first is greater than the character of second
      else{
          int temp = 26 - (first[i] - second[i]);
          // Add the difference/K to cnt
          if (temp >= K)
              cnt = temp / K;
          // Add the difference%K to the cnt
          cnt += (temp % K);
      }
      total += cnt;
   }
   return total;
}
int main(){
   string first = "ACSQ", second = "BCOS";
   int K = 7;
   cout << "The total number of minimum operations require to convert the first string to second are " << totalOperations(first, second, K);
}

Output

The total number of minimum operations require to convert the first string to second are 7

Time complexity – O(N) as we convert all characters of the first string to the second string.

Space complexity – O(1) as we don’t use any constant space.

In this problem, we converted the first string to the second string by performing the cyclic increment operations. Programmers can try to solve the problem in which a total number of cyclic decrement operations are required to convert the first string to the second string.

Updated on: 18-Aug-2023

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements