📌  相关文章
📜  最小化操作以使一个字符串仅包含来自其他字符串的字符

📅  最后修改于: 2022-05-13 01:57:07.471000             🧑  作者: Mango

最小化操作以使一个字符串仅包含来自其他字符串的字符

给定两个仅包含小写英文字母的字符串S1S2 ,任务是最小化使S1仅包含来自S2的字符所需的操作数量,其中在每个操作中 S1 的任何字符都可以转换为任何其他字母,并且成本这两个字母的操作会有所不同

例子:

方法:这个想法是找到使S1的每个字符与S2的任何最接近的字符所需的最小操作数。请按照以下步骤解决问题:

  • 初始化一个变量,例如minOpr0 ,它存储所需的最小操作数。
  • 使用变量i迭代范围[0, N1)并执行以下步骤:
    • 检查字符S1[i]是否存在于S2 中。如果不存在,则继续迭代。
    • 使用变量j在范围[0, 26)上进行迭代。
      • 检查S1[i]是否大于S2[j]然后找到curMinOpr最小值(S1[i] – S2[j])(26 – (S1[i]-'a') + (S2[j] – 'a'))并将值存储在curMinOpr中。
      • 否则,找到curMinOpr最小值(S2[j] – S1[i])((S1[i] – 'a') + (26 – (S2[j] – 'a')))并存储该值在curMinOpr中。
    • 将 minOpr 的值更新为minOpr += curMinOpr
  • 最后,打印minOpr的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum number of
// operations required to make string S1
// contains only characters from the string S2
void minOperations(string S1, string S2,
                   int N1, int N2)
{
    // Stores the minimum of operations
    // required
    int minOpr = 0;
 
    for (int i = 0; i < N1; i++) {
 
        // Check character S1[i] is not
        // present in S2
        if (S2.find(S1[i]) != string::npos) {
            continue;
        }
 
        // Stores the minimum operations required
        // for the current character S1[i]
        int curMinOpr = INT_MAX;
 
        for (int j = 0; j < N2; j++) {
 
            // Check S1[i] alphabet is greater
            // than the S2[j] alphabet
            if (S1[i] > S2[j]) {
 
                // Find the minimum operations
                // required to make the
                // character S1[i] to S2[j]
                curMinOpr
                    = min(curMinOpr,
                          (min(S1[i] - S2[j],
                               26 - (S1[i] - 'a')
                                   + (S2[j] - 'a'))));
            }
            else {
 
                // Find the minimum operations
                // required to make the
                // character S1[i] to S2[j]
                curMinOpr = min(
                    curMinOpr,
                    (min(S2[j] - S1[i],
                         (S1[i] - 'a')
                             + (26 - (S2[j] - 'a')))));
            }
        }
 
        // Update the value of minOpr
        minOpr += curMinOpr;
    }
 
    // Print the value of minOpr
    cout << minOpr << endl;
}
 
// Driver code
int main()
{
    string S1 = "abc", S2 = "ad";
    int N1 = S1.length(), N2 = S2.length();
 
    minOperations(S1, S2, N1, N2);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the minimum number of
// operations required to make String S1
// contains only characters from the String S2
static void minOperations(String S1, String S2,
                   int N1, int N2)
{
   
    // Stores the minimum of operations
    // required
    int minOpr = 0;
 
    for (int i = 0; i < N1; i++) {
 
        // Check character S1.charAt(i) is not
        // present in S2
        if (S2.contains(String.valueOf(S1.charAt(i)))) {
            continue;
        }
 
        // Stores the minimum operations required
        // for the current character S1.charAt(i)
        int curMinOpr = Integer.MAX_VALUE;
 
        for (int j = 0; j < N2; j++) {
 
            // Check S1.charAt(i) alphabet is greater
            // than the S2.charAt(j) alphabet
            if (S1.charAt(i) > S2.charAt(j)) {
 
                // Find the minimum operations
                // required to make the
                // character S1.charAt(i) to S2.charAt(j)
                curMinOpr
                    = Math.min(curMinOpr,
                          (Math.min(S1.charAt(i) - S2.charAt(j),
                               26 - (S1.charAt(i) - 'a')
                                   + (S2.charAt(j) - 'a'))));
            }
            else {
 
                // Find the minimum operations
                // required to make the
                // character S1.charAt(i) to S2.charAt(j)
                curMinOpr = Math.min(
                    curMinOpr,
                    (Math.min(S2.charAt(j) - S1.charAt(i),
                         (S1.charAt(i) - 'a')
                             + (26 - (S2.charAt(j) - 'a')))));
            }
        }
 
        // Update the value of minOpr
        minOpr += curMinOpr;
    }
 
    // Print the value of minOpr
    System.out.print(minOpr +"\n");
}
 
// Driver code
public static void main(String[] args)
{
    String S1 = "abc", S2 = "ad";
    int N1 = S1.length(), N2 = S2.length();
 
    minOperations(S1, S2, N1, N2);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python code for the above approach
def present(S2, c):
    for i in range(len(S2)):
        if S2[i] == c:
            return 1
    return 0
 
# Function to find the minimum number of
# operations required to make string S1
# contains only characters from the string S2
def minOperations(S1, S2, N1, N2):
 
    # Stores the minimum of operations
    # required
    minOpr = 0
 
    for i in range(N1):
 
        # Check character S1[i] is not
        # present in S2
        if present(S2, S1[i]):
            continue
 
        # Stores the minimum operations required
        # for the current character S1[i]
        curMinOpr = 10 ** 9
 
        for j in range(N2):
 
            # Check S1[i] alphabet is greater
            # than the S2[j] alphabet
            if ord(S1[i]) > ord(S2[j]):
 
                # Find the minimum operations
                # required to make the
                # character S1[i] to S2[j]
                curMinOpr = min(
                    curMinOpr,
                    (
                        min(
                            ord(S1[i]) - ord(S2[j]),
                            26
                            - (ord(S1[i]) - ord("a"))
                            + (ord(S2[j]) - ord("a")),
                        )
                    ),
                )
 
            else:
                # Find the minimum operations
                # required to make the
                # character S1[i] to S2[j]
                curMinOpr = min(
                    curMinOpr,
                    (
                        min(
                            ord(S2[j]) - ord(S1[i]),
                            (ord(S1[i]) - ord("a"))
                            + (26 - (ord(S2[j]) - ord("a"))),
                        )
                    ),
                )
 
        # Update the value of minOpr
        minOpr += curMinOpr
 
    # Print the value of minOpr
    print(minOpr)
 
# Driver code
S1 = "abc"
S2 = "ad"
N1 = len(S1)
N2 = len(S2)
 
minOperations(S1, S2, N1, N2)
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
class GFG
{
     
static bool present(string S2, char c) {
    for (int i = 0; i < S2.Length; i++) {
        if (S2[i] == c) {
            return true;
        }
    }
    return false;
}
     
// Function to find the minimum number of
// operations required to make string S1
// contains only characters from the string S2
static void minOperations(string S1, string S2,
                   int N1, int N2)
{
   
    // Stores the minimum of operations
    // required
    int minOpr = 0;
 
    for (int i = 0; i < N1; i++) {
 
        // Check character S1[i] is not
        // present in S2
        if (present(S2, S1[i])) {
            continue;
        }
 
        // Stores the minimum operations required
        // for the current character S1[i]
        int curMinOpr = Int32.MaxValue;
 
        for (int j = 0; j < N2; j++) {
 
            // Check S1[i] alphabet is greater
            // than the S2[j] alphabet
            if (S1[i] > S2[j]) {
 
                // Find the minimum operations
                // required to make the
                // character S1[i] to S2[j]
                curMinOpr
                    = Math.Min(curMinOpr,
                          (Math.Min(S1[i] - S2[j],
                               26 - (S1[i] - 'a')
                                   + (S2[j] - 'a'))));
            }
            else {
 
                // Find the minimum operations
                // required to make the
                // character S1[i] to S2[j]
                curMinOpr = Math.Min(
                    curMinOpr,
                    (Math.Min(S2[j] - S1[i],
                         (S1[i] - 'a')
                             + (26 - (S2[j] - 'a')))));
            }
        }
 
        // Update the value of minOpr
        minOpr += curMinOpr;
    }
 
    // Print the value of minOpr
    Console.WriteLine(minOpr);
}
 
// Driver code
public static void Main()
{
    string S1 = "abc", S2 = "ad";
    int N1 = S1.Length, N2 = S2.Length;
 
    minOperations(S1, S2, N1, N2);
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
2

时间复杂度: O(N 1 * N 2 ) 其中 N 1和 N 2分别是 S1 和 S2 的大小
辅助空间: O(1)