📌  相关文章
📜  将字符串转换为另一个给定字符串所需的最小增量为1或K

📅  最后修改于: 2021-04-17 16:58:20             🧑  作者: Mango

给定两个都由N个大写字母和一个整数K组成的字符串XY ,任务是找到将字符串X转换为字符串Y所需的字符串X中任何字符的循环增量的最小计数乘以1K。

例子:

方法:请按照以下步骤解决此问题:

  • 初始化一个变量,例如count ,以存储将字符串X转换为字符串Y所需的最小增量。
  • 遍历字符串X和字符串X的每一个字符,有以下三种情况:
    • 当字符串XY中的字符相同时,继续。
    • 当在字符串Y大于X字符串然后添加值(值Y [i] – X [I])字符/ K(值Y [i] – X [I])K计数
    • 当在字符串X字符比字符串大于y时,从90减去字符X [i]和从值Y [i]中减去64来计算循环差,然后添加K差/ K计数
  • 完成上述步骤后,将打印计数作为所需的最小增量数。

下面是上述方法的实现:

C++
// CPP program for the above approach
#include
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.


输出:
11

时间复杂度: O(N)
辅助空间: O(N)