Longest Palindromic Subsequence | Practice | GeeksforGeeks

Given a String, find the longest palindromic subsequence.

Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.

Example 2:
Input: 
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.


Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.

Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).

Constraints:
1 ≤ |S| ≤ 1000
0
aamitprasad6185 hours ago
CPP
 
int lcs(string s1,string s2){
        
        int n=s1.length();
        
        int dp[n+1][n+1];
        
        for(int i=0;i<=n;i++){
            for(int j=0;j<=n;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[i-1]==s2[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[n][n];
        
    }
  
  
    int longestPalinSubseq(string A) {
        
        reverse(A.begin(),A.end());
        string B=A;
        reverse(A.begin(),A.end());
        
        int ans=lcs(A,B);
        
        return ans;
    }
0
tarangpatel149993 days ago
 int dp[1001][1001];   int lcs(string a,string b,int n,int m)   {       if(n==0||m==0)       {           return 0;       }       if(dp[n][m]!=-1)        return dp[n][m];            if(a[n-1]==b[m-1])       {          return dp[n][m]=1+lcs(a,b,n-1,m-1);       }       else       {           return dp[n][m]=max(lcs(a,b,n,m-1),lcs(a,b,n-1,m));       }          }   int longestPalinSubseq(string A) {       //code here       memset(dp,-1,sizeof(dp));       string B=A;       reverse(A.begin(),A.end());       return lcs(A,B,A.size(),B.size());   }};
0
chessnoobdj4 days ago
C++
int longestPalinSubseq(string A) {
    string B = A;
    reverse(A.begin(), A.end());
    int n = A.size(), dp[n+1][n+1];
    memset(dp, 0, sizeof(dp));
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            if(A[i-1] == B[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[n][n];
}
+1
2019sushilkumarkori4 days ago
int longestPalinSubseq(string a) {
        //code here
        int n = a.length();
        string b;
        for(int i=n-1;i>=0;i--){
           b.push_back(a[i]); 
        }
        int t[n+1][n+1];
        for(int i=0;i<=n;i++){
            for(int j=0;j<=n;j++){
                if(i==0||j==0){
                    t[i][j] = 0;
                }
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(a[i-1] == b[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 t[n][n];
    }
0
akkeshri140420011 week ago
int lcs(string a,string b,int n,vector<vector<int>>&dp){
     for(int i=0;i<n+1;i++){
         for(int j=0;j<n+1;j++){
             if(i==0 or j==0){
                 dp[i][j]=0;
             }
         }
     }
     for(int i=1;i<n+1;i++){
         for(int j=1;j<n+1;j++){
             if(a[i-1]==b[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[n][n];
 }
   int longestPalinSubseq(string A) {
       //code here
       int n=A.length();
       string B=A;
       reverse(A.begin(),A.end());
       vector<vector<int>>dp(n+1,vector<int>(n+1));
       return lcs(B,A,n,dp);
   }
0
dev1711 week ago
C++|| Same as LCS just second string is reverse of first
  int longestPalinSubseq(string s) {
        //code here
          int n=s.size();
        vector<vector<int>>dp(n+1,vector<int>(n+1,0));
        
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(s[i-1]==s[n-j]){
                    dp[i][j]=1+dp[i-1][j-1];
                }else{
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[n][n];
    }
+1
ananyadixit20012 weeks ago
We can form a second string from string 1 by reversing it and find LCS of both to find the LPS
a= ananya b=aynana – lcs of both will be anana
 
 
int longestPalinSubseq(string s) {
string b=s;         int m=s.length();   int n=b.length();       reverse(s.begin(),s.end());          int t[n+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(s[i-1]==b[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 t[n][n];   }
0
parasdhanwal2 weeks ago
Java | DP
class Solution
{
    public int longestPalinSubseq(String a)
    {
    String b="";
        
        for (int i=0; i<a.length(); i++)
         {
            char ch= a.charAt(i); 
            b=ch+b; 
         }
        int x=a.length();        
        int dp[][]=new int[x][x];
        
        for(int rows[]:dp)
        Arrays.fill(rows,-1);
        
        return lcs(x-1,x-1,a,b,dp);
    }
    
     int lcs(int ind1,int ind2,String a,String b,int [][]dp)
    {
        if(ind1<0 || ind2<0) return 0;
        
        if(dp[ind1][ind2]!=-1) return dp[ind1][ind2];
        
        if(a.charAt(ind1)==b.charAt(ind2)) 
        return dp[ind1][ind2]=1+lcs(ind1-1,ind2-1,a,b,dp);
        
        else
        return dp[ind1][ind2]=Math.max(lcs(ind1-1,ind2,a,b,dp),lcs(ind1,ind2-1,a,b,dp));
    }
}
+1
ayushsunariya4582 weeks ago
Easy Dp C++ Solution
	int longestPalinSubseq(string s1) {
        //code here
        string s2=s1;    //just like lcs aditya video 
        reverse(s2.begin(),s2.end());
        int x=s1.size();
        int y=s2.size();
        int dp[x+1][y+1];
        for(int i=0;i<=x;i++){
            dp[i][0]=0;
        }
        for(int j=0;j<=y;j++){
            dp[0][j]=0;
        }
        
        for(int i = 1; i <= x; i++){
            
            for(int j = 1; j <= y; j++)
            {
                if(s1[i-1] == s2[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[x][y];
    }
0
roboto7o32oo32 weeks ago
Clean C++ Solution ✨ ✨
 
class Solution{
  public:
    int longestPalinSubseq(string A) {
        int n = A.size();
        vector<vector<int>> dp(n, vector<int>(n, 0));
        
        for(int i=0; i<n; i++){
            dp[i][i] = 1;
        }
        
        int k = 1, i, j;
        
        while(k < n){
            i = 0;
            j = k;
            
            while(j<n){
                if(A[i] == A[j]){
                    dp[i][j] = 2 + dp[i+1][j-1];
                }
                else{
                    dp[i][j] = max(dp[i+1][j], dp[i][j-1]);
                }
                
                i++;
                j++;
            }
            
            k++;
        }
        
        return dp[0][n-1];
    }
};
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.