
- 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
Minimum Insertion Steps to Make a String Palindrome in C++
Suppose we have a string s, we have to make this string palindrome. in each step we can insert any character at any position, we have to find minimum number of characters that require to make this palindrome. If the string is like “mad”, then the answer will be 2 as we can add “da” before “mad”, or “am” after “mad” to make this palindrome.
To solve this, we will follow these steps −
- Define a function lcs(), this will take s, x := s
- n := size of s
- reverse the string x
- s := concatenate space before s, x := concatenate space before x
- Define one 2D array dp of size (n + 1) x (n + 1)
- for initialize i := 1, when i <= n, update (increase i by 1), do −
- for initialize j := 1, when j <= n, update (increase j by 1), do −
- dp[i, j] := maximum of dp[i – 1, j] and dp[i, j - 1]
- if s[i] is same as x[j], then −
- dp[i, j] := maximum of dp[i, j] and dp[i – 1, j - 1] + 1
- for initialize j := 1, when j <= n, update (increase j by 1), do −
- return dp[n, n]
- From the main method return size of s – lcs(s)
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int lcs(string s){ string x = s; int n = s.size(); reverse(x.begin(), x.end()); s = " " + s; x = " " + x; vector < vector <int> > dp(n + 1, vector <int>(n + 1)); for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if(s[i] == x[j]){ dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); } } } return dp[n][n]; } int minInsertions(string s) { return s.size() - lcs(s); } }; main(){ Solution ob; cout << (ob.minInsertions("mad")); }
Input
“mad”
Output
2
- Related Articles
- Minimum number of deletions to make a string palindrome in C++.
- Minimum steps to delete a string after repeated deletion of palindrome substrings in C++
- Minimum number of Appends needed to make a string palindrome in C++
- Counting steps to make number palindrome in JavaScript
- Minimum removal to make palindrome permutation in C++
- Program to check minimum number of characters needed to make string palindrome in Python
- Finding minimum steps to make array elements equal in JavaScript
- Minimum Number of Steps to Make Two Strings Anagram in C++
- Minimum steps to remove substring 010 from a binary string in C++
- Find minimum number of merge operations to make an array palindrome in C++
- Program to count number of minimum swaps required to make it palindrome in Python
- Minimum swaps required to make a binary string alternating in C++
- Program to find minimum number of characters to be added to make it palindrome in Python
- Minimum steps to make all the elements of the array divisible by 4 in C++
- Program to find minimum deletions to make string balanced in Python

Advertisements