Minimum Deletions | Practice | GeeksforGeeks

Given a string of S as input. Your task is to write a program to remove or delete the minimum number of characters from the string so that the resultant string is a palindrome.
Note: The order of characters in the string should be maintained.
Example 1:
Input: S = "aebcbda"
Output: 2
Explanation: Remove characters 'e' 
and 'd'.
Example 2:
Input: S = "geeksforgeeks"
Output: 8
Explanation: One of the possible result
string can be "eefee", so answer is 
13 - 5 = 8.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function minimumNumberOfDeletions() which takes the string S as inputs and returns the minimum number of deletions required to convert S into a pallindrome.

Expected Time Complexity: O(|S|2)
Expected Auxiliary Space: O(|S|2)

Constraints:
1 ≤ |S| ≤ 103
0
chessnoobdj3 days ago
C++ bottom-up
int minimumNumberOfDeletions(string S) { 
        int n = S.size();
        int dp[n+1][n+1];
        memset(dp, 0, sizeof(dp));
        for(int i=0; i<=n; i++)
            dp[i][i] = 1;
        for(int i=0; i<n; i++){
            for(int j=i-1; j>=0; j--){
                dp[i][j] = max(dp[i-1][j], dp[i][j+1]);
                if(S[i] == S[j])
                    dp[i][j] = max(dp[i][j], 2+dp[i-1][j+1]);
            }
        }
        return n-dp[n-1][0];
    } 
0
dharpranoy22551 month ago
Python Solution: 0.77/1.22
class Solution:   def delete(s,smap):       if s==s[::-1]:           return 0       if smap.get(s,-1)==-1:           i,j=0,len(s)-1           while s[i]==s[j]:               i+=1               j-=1           if s[i]!=s[j]:               l=Solution.delete(s[i+1:j+1],smap)               r=Solution.delete(s[i:j],smap)               smap[s]=min(r,l)+1       return smap[s]   def minimumNumberOfDeletions(self,S):       smap={}       return Solution.delete(S,smap)
0
patildhiren442 months ago
Java - Tabulation
static int lps(String s, String t, int n){
        int[][] dp = new int[n+1][n+1];
        
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                
                if(s.charAt(i-1)==t.charAt(j-1)){
                    dp[i][j] = 1 + dp[i-1][j-1];
                }else{
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        return dp[n][n];
    }
    
    static int minimumNumberOfDeletions(String s) {
        //your code here
        int n = s.length();
        
        String t = new StringBuilder(s).reverse().toString();
        
        return n - lps(s, t, n);
    }
0
ameenul88083 months ago
Optimized DP solution (1-d space)
int minimumNumberOfDeletions(string s) { 
        // code here
        string p = s;
        int m = s.length();
        reverse(s.begin(),s.end());
        vector<int> prev(m+1,0);
        
        for(int i=1;i<=m;i++){
            vector<int> curr(m+1,0);
            for(int j=1;j<=m;j++){
                if(p[i-1]==s[j-1]) curr[j] = 1 + prev[j-1];
                else curr[j] = max( prev[j],  curr[j-1]);
            }
            prev = curr;
        }
        return m-prev[m];
    } 
0
pankajsinghrajput3 months ago
//SIMPLE DP SOL
class Solution{   int lcs(string s,string str)   {       int m=s.size();       int n=str.size();       int dp[m+1][n+1];       for(int i=0;i<m+1;i++)       {           for(int j=0;j<n+1;j++)           {               if(i==0||j==0)               dp[i][j]=0;           }       }       for(int i=1;i<m+1;i++)       {           for(int j=1;j<n+1;j++)           {               if(s[i-1]==str[j-1])               {                   dp[i][j]=1+dp[i-1][j-1];               }               else               {                   dp[i][j]=max(dp[i-1][j],dp[i][j-1]);               }           }       }       return dp[m][n];   }
 
 public:   int minimumNumberOfDeletions(string S) {        // code here       string str=S;       reverse(str.begin(),str.end());       int l=lcs(S,str);       return S.size()-l;   } };
 
0
geminicode4 months ago
my approach in python!!
def minimumNumberOfDeletions(self,S):
        # code here 
        
        dp = [[0 for i in range(len(S))] for j in range(len(S))]
        for g in range(len(S)):
            for i,j in zip(range(0,len(S)-g),range(g,len(S))):
                if g == 0 :
                    dp[i][j] = 0
                elif g==1:
                    if S[i] == S[j] :
                        dp[i][j] = 0
                    else:
                        dp[i][j] = 1
                else:
                    if S[i] == S[j]:
                        dp[i][j] = dp[i+1][j-1]
                    else:
                        dp[i][j] = 1 + min(dp[i+1][j],dp[i][j-1])
        return dp[0][len(S)-1]
+2
jhav16434 months ago
C++ Top Down Approach in 0.0/1.1
int minimumNumberOfDeletions(string s) {        string str = s;              reverse(str.begin(), str.end());              int n = s.length();              int ans[n+1][n+1];              for(int i=0; i<=n; i++)       {           for(int j=0; j<=n; j++)           {               if(i==0 || j==0)               {                   ans[i][j] = 0;               }                              else if(s[i-1] == str[j-1])               {                   ans[i][j] = 1+ans[i-1][j-1];               }                              else               {                   ans[i][j] = max(ans[i][j-1], ans[i-1][j]);               }           }       }              return n - ans[n][n];   }
0
sikkusaurav1235 months ago
/////////---------------top + down approch----------------//////////////
public:   int minimumNumberOfDeletions(string s1) {        string s2=s1;       reverse(s2.begin(),s2.end());       int m=s1.length();       int n=s2.length();       int t[m+1][n+1];        for(int i=0;i<m+1;i++)  {      for(int j=0;j<n+1;j++)      {          if(i==0||j==0)          {              t[i][j]=0;          }      }  }  for(int i=1;i<m+1;i++)  {      for(int j=1;j<n+1;j++)      {          if(s1[i-1]==s2[j-1])          {              t[i][j]=1+t[i-1][j-1];          }          else          {              t[i][j]=max(t[i-1][j],t[i][j-1]);          }      }  }  return m-t[m][n];           } 
0
bhargav_417 months ago
Java Solution | LCS Bottom-Up Approach
 
class Solution{
    static int minimumNumberOfDeletions(String s1) {
        int n = s1.length();
        StringBuilder sb = new StringBuilder(s1);
        sb.reverse();
        String s2 = sb.toString();
        int[][] dp = new int[n+1][n+1];
        for(int i=0;i<n+1;i++){
            for(int j=0;j<n+1;j++){
                if(i==0 || j==0) dp[i][j] = 0;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(s1.charAt(i-1)==s2.charAt(j-1)) dp[i][j] = 1+ dp[i-1][j-1];
                else dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
            }
        }
        return n-dp[n][n];
    }
}
0
s37randive8 months ago
int minimumNumberOfDeletions(string s) { 
       // code here
       string t=string(s.rbegin(),s.rend());
       int dp[s.length()+1][s.length()+1];
       for(int i=0;i<s.length()+1;i++){
           dp[i][0]=0;
       }
       for(int i=1;i<s.length()+1;i++){
           dp[0][i]=0;
       }
       for(int i=1;i<s.length()+1;i++){
           for(int j=1;j<s.length()+1;j++){
               if(s[i-1]==t[j-1]){
                   dp[i][j]=1+dp[i-1][j-1];
               }else{
                   dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
               }
           }
       }
       return s.length()-dp[s.length()][s.length()];
   } 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.