516. Longest Palindromic Subsequence
pls upvote  if it was helpful :)

class Solution {
public:
    int longestPalindromeSubseq(string s) 
    {
        //LCS:
        string r = s;
        reverse(r.begin(),r.end()); // reverse of original string(s):
        
        int m=s.size();
        int n=r.size();
        int dp[m+1][n+1]; //Dp Matrix:
        
        for (int i=0;i<n+1;i++)  //initializing  row as zero:
        {
            dp[0][i]=0;
        }
        for (int j=0;j<m+1;j++)  //initializing  column as zero:
        {
            dp[j][0]=0;
        }
        
        for (int i=1;i<m+1;i++)
        {
            for (int j=1;j<n+1;j++)
            {
                if (s[i-1] == r[j-1]) // if character of both string are same:
                {
                    dp[i][j] = 1 + dp[i-1][j-1]; // one plus its diagonal value
                }
                else
                {
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]); //maximum from row and column;
                }
            }
        }
        
        return dp[m][n]; 
  
    }
};
Comments (1)