📌  相关文章
📜  将一个字符串转换为另一个字符串的最小删除和插入次数

📅  最后修改于: 2021-04-29 09:45:11             🧑  作者: Mango

给定两个分别为m和n的字符串’str1’和’str2’。任务是从str1中删除/删除并插入最少数量的字符,以将其转换为str2。有可能需要从str1的一个点删除/删除相同的字符,然后再将其插入另一个点。

范例1:

Input : 
str1 = "heap", str2 = "pea" 
Output : 
Minimum Deletion = 2 and
Minimum Insertion = 1
Explanation:
p and h deleted from heap
Then, p is inserted at the beginning
One thing to note, though p was required yet
it was removed/deleted first from its position and
then it is inserted to some other position.
Thus, p contributes one to the deletion_count
and one to the insertion_count.

豌豆的图示

范例2:

Input : 
str1 = "geeksforgeeks", str2 = "geeks"
Output : 
Minimum Deletion = 8
Minimum Insertion = 0       

geeksforgeeks的图形表示

简单方法:

一种简单的方法是考虑str1的所有子序列,并为每个子序列计算最小的缺失和插入,以将其转换为str2。一个非常复杂的方法和此解决方案的时间复杂度是指数级的。

高效方法:

一种有效的方法使用找到给定两个序列的最长公共子序列的长度的概念。

算法:

  • str1str2是给定的字符串。
  • mn分别是它们的长度。
  • lenstr1str2的最长公共子序列的长度
  • 最小删除数minDel = m – len
  • 最小插入次数minInsert = n – len

下面是上述代码的实现:

C++
// Dynamic Programming C++ implementation to find
// minimum number of deletions and insertions
#include 
 
using namespace std;
 
// Returns length of length common subsequence
// for str1[0..m-1], str2[0..n-1]
int lcs(string str1, string str2, int m, int n)
{
    int L[m + 1][n + 1];
    int i, j;
 
    // Following steps build L[m+1][n+1] in bottom
    // up fashion. Note that L[i][j] contains
    // length of LCS of str1[0..i-1] and str2[0..j-1]
    for (i = 0; i <= m; i++) {
        for (j = 0; j <= n; j++) {
            if (i == 0 || j == 0)
                L[i][j] = 0;
 
            else if (str1.at(i - 1) == str2.at(j - 1))
                L[i][j] = L[i - 1][j - 1] + 1;
 
            else
                L[i][j] = max(L[i - 1][j], L[i][j - 1]);
        }
    }
 
    // L[m][n] contains length of LCS
    // for X[0..n-1] and Y[0..m-1]
    return L[m][n];
}
 
// function to find minimum number
// of deletions and insertions
void printMinDelAndInsert(string str1, string str2)
{
    int m = str1.size();
    int n = str2.size();
 
    int len = lcs(str1, str2, m, n);
 
    cout << "Minimum number of deletions = " << (m - len)
         << endl;
 
    cout << "Minimum number of insertions = " << (n - len)
         << endl;
}
 
// Driver Code
int main()
{
    string str1 = "heap";
    string str2 = "pea";
   
      // Function Call
    printMinDelAndInsert(str1, str2);
    return 0;
}


Java
// Dynamic Programming Java implementation
// to find minimum number of deletions and
// insertions
import java.io.*;
 
class GFG {
 
    // Returns length of length common
    // subsequence for str1[0..m-1],
    // str2[0..n-1]
    static int lcs(String str1, String str2, int m, int n)
    {
        int L[][] = new int[m + 1][n + 1];
        int i, j;
 
        // Following steps build L[m+1][n+1] in
        // bottom up fashion. Note that L[i][j]
        // contains length of LCS of str1[0..i-1]
        // and str2[0..j-1]
        for (i = 0; i <= m; i++) {
            for (j = 0; j <= n; j++) {
                if (i == 0 || j == 0)
                    L[i][j] = 0;
 
                else if (str1.charAt(i - 1)
                         == str2.charAt(j - 1))
                    L[i][j] = L[i - 1][j - 1] + 1;
 
                else
                    L[i][j] = Math.max(L[i - 1][j],
                                       L[i][j - 1]);
            }
        }
 
        // L[m][n] contains length of LCS
        // for X[0..n-1] and Y[0..m-1]
        return L[m][n];
    }
 
    // function to find minimum number
    // of deletions and insertions
    static void printMinDelAndInsert(String str1,
                                     String str2)
    {
        int m = str1.length();
        int n = str2.length();
 
        int len = lcs(str1, str2, m, n);
 
        System.out.println("Minimum number of "
                           + "deletions = ");
        System.out.println(m - len);
 
        System.out.println("Minimum number of "
                           + "insertions = ");
        System.out.println(n - len);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str1 = new String("heap");
        String str2 = new String("pea");
       
          // Function Call
        printMinDelAndInsert(str1, str2);
    }
}
// This code is contributed by Prerna Saini


Python3
# Dynamic Programming Python3
# implementation to find minimum
# number of deletions and insertions
 
# Returns length of length
# common subsequence for
# str1[0..m-1], str2[0..n-1]
 
 
def lcs(str1, str2, m, n):
 
    L = [[0 for i in range(n + 1)]
         for i in range(m + 1)]
 
    # Following steps build L[m+1][n+1]
    # in bottom up fashion. Note that
    # L[i][j] contains length of LCS
    # of str1[0..i-1] and str2[0..j-1]
    for i in range(m + 1):
        for j in range(n + 1):
            if (i == 0 or j == 0):
                L[i][j] = 0
            elif(str1[i - 1] == str2[j - 1]):
                L[i][j] = L[i - 1][j - 1] + 1
            else:
                L[i][j] = max(L[i - 1][j],
                              L[i][j - 1])
 
    # L[m][n] contains length of LCS
    # for X[0..n-1] and Y[0..m-1]
    return L[m][n]
 
# function to find minimum number
# of deletions and insertions
 
 
def printMinDelAndInsert(str1, str2):
    m = len(str1)
    n = len(str2)
    leng = lcs(str1, str2, m, n)
    print("Minimum number of deletions = ",
          m - leng, sep=' ')
    print("Minimum number of insertions = ",
          n - leng, sep=' ')
 
 
# Driver Code
str1 = "heap"
str2 = "pea"
 
# Function Call
printMinDelAndInsert(str1, str2)
 
# This code is contributed
# by sahilshelangia


C#
// Dynamic Programming C# implementation
// to find minimum number of deletions and
// insertions
using System;
 
class GFG {
 
    // Returns length of length common
    // subsequence for str1[0..m-1],
    // str2[0..n-1]
    static int lcs(string str1, string str2, int m, int n)
    {
        int[, ] L = new int[m + 1, n + 1];
        int i, j;
 
        // Following steps build L[m+1][n+1] in
        // bottom up fashion. Note that L[i][j]
        // contains length of LCS of str1[0..i-1]
        // and str2[0..j-1]
        for (i = 0; i <= m; i++) {
            for (j = 0; j <= n; j++) {
                if (i == 0 || j == 0)
                    L[i, j] = 0;
 
                else if (str1[i - 1] == str2[j - 1])
                    L[i, j] = L[i - 1, j - 1] + 1;
 
                else
                    L[i, j] = Math.Max(L[i - 1, j],
                                       L[i, j - 1]);
            }
        }
 
        // L[m][n] contains length of LCS
        // for X[0..n-1] and Y[0..m-1]
        return L[m, n];
    }
 
    // function to find minimum number
    // of deletions and insertions
    static void printMinDelAndInsert(string str1,
                                     string str2)
    {
        int m = str1.Length;
        int n = str2.Length;
 
        int len = lcs(str1, str2, m, n);
 
        Console.Write("Minimum number of "
                      + "deletions = ");
        Console.WriteLine(m - len);
 
        Console.Write("Minimum number of "
                      + "insertions = ");
        Console.Write(n - len);
    }
 
    // Driver code
    public static void Main()
    {
        string str1 = new string("heap");
        string str2 = new string("pea");
       
          // Function Call
        printMinDelAndInsert(str1, str2);
    }
}
 
// This code is contributed by nitin mittal.


输出
Minimum number of deletions = 2
Minimum number of insertions = 1

时间复杂度: O(m * n)