Minimum number of deletions to make a string palindrome
Given a string of size ‘n’. The task is to remove or delete minimum number of characters from the string so that the resultant string is palindrome.
Note: The order of characters should be maintained.
Examples :
Input : aebcbda Output : 2 Remove characters 'e' and 'd' Resultant string will be 'abcba' which is a palindromic string Input : geeksforgeeks Output : 8
A simple solution is to remove all subsequences one by one and check if remaining string is palindrome or not. Time complexity of this solution is exponential.
An efficient approach uses the concept of finding the length of the longest palindromic subsequence of a given sequence.
Algorithm:
-->str is the given string. -->n length of str -->len be the length of the longest palindromic subsequence of str -->// minimum number of deletions min = n - len
C++
// C++ implementation to find // minimum number of deletions // to make a string palindromic #include <bits/stdc++.h> using namespace std; // Returns the length of // the longest palindromic // subsequence in 'str' int lps(string str) { int n = str.size(); // Create a table to store // results of subproblems int L[n][n]; // Strings of length 1 // are palindrome of length 1 for (int i = 0; i < n; i++) L[i][i] = 1; // Build the table. Note that // the lower diagonal values // of table are useless and // not filled in the process. // c1 is length of substring for (int cl = 2; cl <= n; cl++) { for (int i = 0; i < n - cl + 1; i++) { int j = i + cl - 1; if (str[i] == str[j] && cl == 2) L[i][j] = 2; else if (str[i] == str[j]) L[i][j] = L[i + 1][j - 1] + 2; else L[i][j] = max(L[i][j - 1], L[i + 1][j]); } } // length of longest // palindromic subseq return L[0][n - 1]; } // function to calculate // minimum number of deletions int minimumNumberOfDeletions(string str) { int n = str.size(); // Find longest palindromic // subsequence int len = lps(str); // After removing characters // other than the lps, we // get palindrome. return (n - len); } // Driver Code int main() { string str = "geeksforgeeks"; cout << "Minimum number of deletions = " << minimumNumberOfDeletions(str); return 0; } |
Java
// Java implementation to // find minimum number of // deletions to make a // string palindromic class GFG { // Returns the length of // the longest palindromic // subsequence in 'str' static int lps(String str) { int n = str.length(); // Create a table to store // results of subproblems int L[][] = new int[n][n]; // Strings of length 1 // are palindrome of length 1 for (int i = 0; i < n; i++) L[i][i] = 1; // Build the table. Note // that the lower diagonal // values of table are useless // and not filled in the process. // c1 is length of substring for (int cl = 2; cl <= n; cl++) { for (int i = 0; i < n - cl + 1; i++) { int j = i + cl - 1; if (str.charAt(i) == str.charAt(j) && cl == 2) L[i][j] = 2; else if (str.charAt(i) == str.charAt(j)) L[i][j] = L[i + 1][j - 1] + 2; else L[i][j] = Integer.max(L[i][j - 1], L[i + 1][j]); } } // length of longest // palindromic subsequence return L[0][n - 1]; } // function to calculate minimum // number of deletions static int minimumNumberOfDeletions(String str) { int n = str.length(); // Find longest palindromic // subsequence int len = lps(str); // After removing characters // other than the lps, we get // palindrome. return (n - len); } // Driver Code public static void main(String[] args) { String str = "geeksforgeeks"; System.out.println("Minimum number " + "of deletions = "+ minimumNumberOfDeletions(str)); } } // This code is contributed by Sumit Ghosh |
Python 3
# Python 3 implementation to find # minimum number of deletions # to make a string palindromic # Returns the length of # the longest palindromic # subsequence in 'str' def lps(str): n = len(str) # Create a table to store # results of subproblems L = [[0 for x in range(n)]for y in range(n)] # Strings of length 1 # are palindrome of length 1 for i in range(n): L[i][i] = 1 # Build the table. Note that # the lower diagonal values # of table are useless and # not filled in the process. # c1 is length of substring for cl in range( 2, n+1): for i in range(n - cl + 1): j = i + cl - 1 if (str[i] == str[j] and cl == 2): L[i][j] = 2 elif (str[i] == str[j]): L[i][j] = L[i + 1][j - 1] + 2 else: L[i][j] = max(L[i][j - 1],L[i + 1][j]) # length of longest # palindromic subseq return L[0][n - 1] # function to calculate # minimum number of deletions def minimumNumberOfDeletions( str): n = len(str) # Find longest palindromic # subsequence l = lps(str) # After removing characters # other than the lps, we # get palindrome. return (n - l) # Driver Code if __name__ == "__main__": str = "geeksforgeeks" print( "Minimum number of deletions = " , minimumNumberOfDeletions(str)) |
C#
// C# implementation to find // minimum number of deletions // to make a string palindromic using System; class GFG { // Returns the length of // the longest palindromic // subsequence in 'str' static int lps(String str) { int n = str.Length; // Create a table to store // results of subproblems int [,]L = new int[n, n]; // Strings of length 1 // are palindrome of length 1 for (int i = 0; i < n; i++) L[i, i] = 1; // Build the table. Note // that the lower diagonal // values of table are useless // and not filled in the process. // c1 is length of substring for (int cl = 2; cl <= n; cl++) { for (int i = 0; i < n - cl + 1; i++) { int j = i + cl - 1; if (str[i] == str[j] && cl == 2) L[i, j] = 2; else if (str[i] == str[j]) L[i, j] = L[i + 1, j - 1] + 2; else L[i, j] = Math.Max(L[i, j - 1], L[i + 1, j]); } } // length of longest // palindromic subsequence return L[0, n - 1]; } // function to calculate minimum // number of deletions static int minimumNumberOfDeletions(string str) { int n = str.Length; // Find longest palindromic // subsequence int len = lps(str); // After removing characters // other than the lps, we get // palindrome. return (n - len); } // Driver Code public static void Main() { string str = "geeksforgeeks"; Console.Write("Minimum number of" + " deletions = " + minimumNumberOfDeletions(str)); } } // This code is contributed by nitin mittal. |
PHP
<?php // PHP implementation to find // minimum number of deletions // to make a string palindromic // Returns the length of // the longest palindromic // subsequence in 'str' function lps($str) { $n = strlen($str); // Create a table to store // results of subproblems $L; // Strings of length 1 // are palindrome of length 1 for ($i = 0; $i < $n; $i++) $L[$i][$i] = 1; // Build the table. Note that // the lower diagonal values // of table are useless and // not filled in the process. // c1 is length of substring for ($cl = 2; $cl <= $n; $cl++) { for ( $i = 0; $i < $n -$cl + 1; $i++) { $j = $i + $cl - 1; if ($str[$i] == $str[$j] && $cl == 2) $L[$i][$j] = 2; else if ($str[$i] == $str[$j]) $L[$i][$j] = $L[$i + 1][$j - 1] + 2; else $L[$i][$j] = max($L[$i][$j - 1], $L[$i + 1][$j]); } } // length of longest // palindromic subseq return $L[0][$n - 1]; } // function to calculate minimum // number of deletions function minimumNumberOfDeletions($str) { $n = strlen($str); // Find longest // palindromic subsequence $len = lps($str); // After removing characters // other than the lps, we get // palindrome. return ($n - $len); } // Driver Code { $str = "geeksforgeeks"; echo "Minimum number of deletions = ", minimumNumberOfDeletions($str); return 0; } // This code is contributed by nitin mittal. ?> |
Output :
Minimum number of deletions = 8
Time Complexity : O(n2)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of Appends needed to make a string palindrome
- Minimum number of characters to be replaced to make a given string Palindrome
- Minimum number of deletions to make a sorted sequence
- Minimum number of deletions and insertions to transform one string into another
- Minimum characters to be added at front to make string palindrome
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Find minimum number of merge operations to make an array palindrome
- Number of Counterclockwise shifts to make a string palindrome
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum number of deletions so that no two consecutive are same
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum number of characters to be removed to make a binary string alternate
- Minimum removal to make palindrome permutation
- Remove a character from a string to make it a palindrome



