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

📅  最后修改于: 2021-04-29 03:51:02             🧑  作者: Mango

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

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

例子:

方法:想法是执行交换操作最大次数以减少总成本。观察到,这是常见的之间的字符串AB可以进行交换的任何数量的次使字符串中的字符等于b。所有这一切都出现在字符串中的但不是在字符串B中的字符必须已删除所有目前在B和不存在字符已经要插入以使两个字符串相等。请按照以下步骤解决问题:

  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


输出:
7

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