
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all possible ways to convert one string into another string in C++
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
#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
- Related Articles
- Check if it is possible to convert one string into another with given constraints in Python
- Print all ways to break a string in bracket form in C++
- Print array of strings in sorted order without copying one string into another in C++
- Program to get operations to convert one string to another in C++
- How to copy a String into another String in C#
- Convert the string into palindrome string by changing only one character in C++
- String Transforms Into Another String in Python
- Check if it is possible to transform one string to another in Python
- Insert a String into another String in Java
- Convert String into Binary Sequence in C++
- Program to check whether one string can be 1-to-1 mapped into another string in Python
- How to convert a string into int in C#?
- Java Program to Insert a string into another string
- Golang program to insert a string into another string
- Python Program to Insert a string into another string
