
- 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 steps to delete a string after repeated deletion of palindrome substrings in C++
Problem statement
Given a string containing characters as integers only. We need to delete all character of this string in a minimum number of steps where in one step we can delete the substring which is a palindrome. After deleting a substring remaining parts are concatenated.
Example
If input string is 3441213 then minimum 2 steps required
- First remove 121 from string. Now remaining string is 3443
- Remove remaining string as it’s palindrome
Algorithm
We can use dynamic programming to solve this problem
1. Let dp[i][j] denotes the number of steps it takes to delete the substring s[i, j] 2. Each character will be deleted alone or as part of some substring so in the first case we will delete the character itself and call subproblem (i+1, j) 3. In the second case we will iterate over all occurrence of the current character in right side, if K is the index of one such occurrence then the problem will reduce to two subproblems (i+1, K – 1) and (K+1, j) 4. We can reach to this subproblem (i+1, K-1) because we can just delete the same character and call for mid substring 5. We need to take care of a case when first two characters are same in that case we can directly reduce to the subproblem (i+2, j)
Example
#include <bits/stdc++.h> using namespace std; int getMinRequiredSteps(string str) { int n = str.length(); int dp[n + 1][n + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = 0; } } for (int len = 1; len <= n; len++) { for (int i = 0, j = len - 1; j < n; i++, j++) { if (len == 1) dp[i][j] = 1; else { dp[i][j] = 1 + dp[i + 1][j]; if (str[i] == str[i + 1]) { dp[i][j] = min(1 + dp[i+ 2][j], dp[i][j]); } for (int K = i + 2; K <= j; K++){ if (str[i] == str[K]) { dp[i][j] = min(dp[i+1][K-1] + dp[K+1][j], dp[i][j]); } } } } } return dp[0][n - 1]; } int main() { string str = "3441213"; cout << "Minimum required steps: " << getMinRequiredSteps(str) << endl; return 0; }
When you compile and execute above program. It generates following output
Output
Minimum required steps: 2
- Related Articles
- Minimum Insertion Steps to Make a String Palindrome in C++
- Is the string a combination of repeated substrings in JavaScript
- Minimum number of deletions to make a string palindrome in C++.
- Minimum number of Appends needed to make a string palindrome in C++
- Minimum deletion sum of characters in JavaScript
- Minimum steps to remove substring 010 from a binary string in C++
- Program to check minimum number of characters needed to make string palindrome in Python
- Forming palindrome using at most one deletion in JavaScript
- C++ program to find string after adding character 'a' string becomes non-palindrome
- Counting steps to make number palindrome in JavaScript
- Program to find minimum length of string after deleting similar ends in Python
- Find K-Length Substrings With No Repeated Characters in Python
- How to delete repeated rows in xts object in R?
- Program to find minimum deletion cost to avoid repeating letters in Python
- Replacing Substrings in a Java String

Advertisements