Minimum no. of deletions & insertions to transform 1 string into another
6872

Eg str1 = "heap", str2 = "pea"
O/P : 1 deletion & 2 insertions
why : Deleting 'h' from 'heap' gives us 'eap' and now deleting 'p' from 'eap' gives us 'ea'
Now inserting 'p' gives us string 2 i.e 'pea'
Approach: Observe carefully that their LCS i.e longest common subsequence of the 2 strings remain same and We need to delete some characters from string 1 to reduce it to LCS and then adding some characters to LCS to convert to string 2
str1 = "heap", str2 = "pea"
image
So :

  • minimum number of deletions = str1.length – LCS
  • minimum number of Insertions minInsert = str2.length – LCS
void MinDelAndInsert(string str1, string str2)
{
    int m = str1.size();
    int n = str2.size();
 
    int LCS_Len = LCS(str1, str2, m, n);
 
    cout << "Minimum number of deletions = " << (m - LCS_len)
         << endl;
 
    cout << "Minimum number of insertions = " << (n - LCS_len)
         << endl;
}
Comments (3)