Print all possible ways to convert one string into another string in C++

C++Server Side ProgrammingProgramming

In this problem, we are given two strings str1 and str2. Our task is to create a program to Print all possible ways to convert one string into another string. 

Problem Description: Here, we need to find all possible ways using which we can convert str1 to str2. While converting, we can perform any of the three operations,

  • Insert 
  • Remove
  • Replace

Let’s take an example to understand the problem, 

Input: str1 = “kfeod” str2 = “kfcadq”

Output

Way1:

Insert q, after d. Replace c by e. Replace o by a.

Solution Approach

We will find the minimum number of edits first and then create a DP matrix. Then check if the character pertaining to the element in both strings is equal, then don’t modify otherwise update as it is copied from the last element.

Here, the current character DP[i][j] = DP[i-1][j-1]. Check if the (i-1)th element of str1 and (j-1)th element of str2 are equal, then traverse the DP diagonally.

Now, if the (i-1)th element of str1 and (j-1)th element of str2 are not equal. The value of DP[i][j] is (minimum value out of DP[i-1][j-1] , DP[i][j-1] andDP[i-1][j]) + 1.

This method can print one way to convert one string to another. The method to print all ways is a bit tricky that uses advanced data structure like a vector of strings and we will learn about it later.

Program to illustrate the working of our approach,

Example

Live Demo

#include <iostream>
using namespace std;

int DP[100][100];

void findWays(string str1, string str2) {
   
   int len1 = str1.length();
   int len2 = str2.length();
   int i, j;
   DP[len1 + 1][len2 + 1];

   for (i = 0; i <= len1; i++)
      DP[i][0] = i;
   for (j = 0; j <= len2; j++)
      DP[0][j] = j;

   for (i = 1; i <= len1; i++) {
      for (j = 1; j <= len2; j++) {
         if (str1[i - 1] == str2[j - 1])
            DP[i][j] = DP[i - 1][j - 1];
         else
            DP[i][j] = (min(min(DP[i - 1][j - 1], DP[i - 1][j]), DP[i][j - 1])) + 1;
      }
   }
   while (len1 and len2) {

      if (str1[len1 - 1] == str2[len2 - 1]) {
         
         len1--;
         len2--;
      }
      else if (DP[len1][len2] == DP[len1-1][len2-1] + 1) {
         
         cout<<"\nModify '"<<str1[len1-1]<<"' to '"<<str2[len2-1];
         len1--;
         len2--;
      }
      else if (DP[len1][len2] == DP[len1-1][len2] + 1) {
         
         cout<<"\nRemove "<<str1[len1-1]<<"'";
         len1--;
      }
      else if (DP[len1][len2] == DP[len1][len2-1] + 1) {
         
         cout <<"\nInsert '"<<str2[len2-1]<<"'";
         len2--;
      }
   }
}

int main() {
   
   string str1 = "kfeodge";
   string str2 = "kfcadqpe";
   cout<<"Way to convert one string into another string is ";
   findWays(str1, str2);
   return 0;
}

Output

Way to convert one string into another string is
Modify 'g' to 'p
Insert 'q'
Modify 'o' to 'a
Modify 'e' to 'c
raja
Published on 25-Jan-2021 09:57:10
Advertisements