📌  相关文章
📜  使用交换、插入或删除操作将一个给定字符串转换为另一个给定字符串的最低成本

📅  最后修改于: 2021-10-27 07:18:57             🧑  作者: Mango

给定两个长度分别为NM 的字符串AB ,任务是使用以下操作找到将字符串A转换为B的最小成本:

  • 字符串A 的一个字符可以与同一个字符串的另一个字符交换。成本 = 0
  • 一个字符可以从字符串B 中删除,也可以插入到字符串A 中。成本 = 1

例子:

方法:想法是执行最多次数的交换操作以降低总成本。观察到字符串AB之间共有的字符可以在A 中交换任意次数以使字符串等于B 。必须从A 中删除出现在字符串A 中但不在字符串B中的所有字符,并且必须将所有出现在B 中但不出现在A 中的字符插入到A 中以使两个字符串相等。请按照以下步骤解决问题:

  1. 初始化两个长度为256 的数组a[]b[]分别存储字符串AB中每个字符的频率。
  2. 初始化一个变量,比如minCost ,以存储最小成本。
  3. 使用变量i遍历范围[0, 255]并在每次迭代中,将minCost增加abs(a[i] – b[i])
  4. 完成上述步骤后,打印minCost的值作为将字符串A转换为B所需的最小成本。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum cost
// to convert string a to b
void minimumCost(string a, string b)
{
    // Stores the frequency of string
    // a and b respectively
    vector fre1(256), fre2(256);
 
    // Store the frequencies of
    // characters in a
    for (char c : a)
        fre1[(int)(c)]++;
 
    // Store the frequencies of
    // characters in b
    for (char c : b)
        fre2[(int)(c)]++;
 
    // Minimum cost to convert A to B
    int mincost = 0;
 
    // Find the minimum cost
    for (int i = 0; i < 256; i++) {
        mincost += abs(fre1[i]
                       - fre2[i]);
    }
 
    // Print the minimum cost
    cout << mincost << endl;
}
 
// Driver Code
int main()
{
    string A = "1AB+-", B = "cc";
 
    // Function Call
    minimumCost(A, B);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the minimum cost
// to convert string a to b
public static void minimumCost(String a, String b)
{
     
    // Stores the frequency of string
    // a and b respectively
    int fre1[] = new int[256];
    int fre2[] = new int[256];
  
    // Store the frequencies of
    // characters in a
    for(char c : a.toCharArray())
        fre1[(int)(c)]++;
  
    // Store the frequencies of
    // characters in b
    for(char c : b.toCharArray())
        fre2[(int)(c)]++;
  
    // Minimum cost to convert A to B
    int mincost = 0;
  
    // Find the minimum cost
    for(int i = 0; i < 256; i++)
    {
        mincost += Math.abs(fre1[i] -
                            fre2[i]);
    }
  
    // Print the minimum cost
    System.out.println(mincost);
}
 
// Driver Code
public static void main(String[] args)
{
    String A = "1AB+-", B = "cc";
     
    // Function Call
    minimumCost(A, B);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
 
# Function to find the minimum cost
# to convert a to b
def minimumCost(a, b):
   
    # Stores the frequency of string
    # a and b respectively
    fre1 = [0]*(256)
    fre2 = [0]*(256)
 
    # Store the frequencies of
    # characters in a
    for c in a:
        fre1[ord(c)] += 1
 
    # Store the frequencies of
    # characters in b
    for c in b:
        fre2[ord(c)] += 1
 
    # Minimum cost to convert A to B
    mincost = 0
 
    # Find the minimum cost
    for i in range(256):
        mincost += abs(fre1[i] - fre2[i])
 
    # Print the minimum cost
    print(mincost)
 
# Driver Code
if __name__ == '__main__':
    A = "1AB+-"
    B = "cc"
 
    # Function Call
    minimumCost(A, B)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to find the minimum cost
// to convert string a to b
public static void minimumCost(string a,
                               string b)
{
     
    // Stores the frequency of string
    // a and b respectively
    int[] fre1 = new int[256];
    int[] fre2 = new int[256];
   
    // Store the frequencies of
    // characters in a
    foreach(char c in a.ToCharArray())
        fre1[(int)(c)]++;
         
    // Store the frequencies of
    // characters in b
    foreach(char c in b.ToCharArray())
        fre2[(int)(c)]++;
         
    // Minimum cost to convert A to B
    int mincost = 0;
     
    // Find the minimum cost
    for(int i = 0; i < 256; i++)
    {
        mincost += Math.Abs(fre1[i] -
                            fre2[i]);
    }
     
    // Print the minimum cost
    Console.Write(mincost);
}
  
// Driver code
public static void Main()
{
    string A = "1AB+-", B = "cc";
      
    // Function Call
    minimumCost(A, B);
}   
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
7

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程