<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>&#39;s length in</em> <code>s</code>.</p>

<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;bbbab&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is &quot;bbbb&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;cbbd&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is &quot;bb&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s</code> consists only of lowercase English letters.</li>
</ul>

class Solution {
public:
int t[1001][1001];
// int LCS(string & s, string & a, int n, int m){
//     if(n==0 || m==0) return 0;
//     if(t[n][m]!=-1) return t[n][m];
//     if(s[n-1]==a[m-1]){
//         return t[n][m]= 1+ LCS(s,a,n-1,m-1);
//     }else{
//         return t[n][m]= max(LCS(s,a,n-1,m),LCS(s,a,n,m-1));
//     }
// }

    int LCS(string text1, string text2) {
         int n=text1.size();
        int m=text2.size();
         int t[n+1][m+1];
        
        for(int i=0;i<n+1;i++){
            for(int j=0;j<m+1;j++){
                if(i==0 || j==0){
                    t[i][j]=0;
                }
            }
        }

        // if(n==0 || m==0) return 0;
        // if(t[n][m]!=-1) return t[n][m];
        for(int i=1;i<n+1;i++){
            for(int j=1;j<m+1;j++){

        if(text1[i-1]==text2[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][m];
      
    }
    

    int longestPalindromeSubseq(string s) {
        memset(t,-1,sizeof(t));
        int n=s.size();
        string reversed(s.rbegin(), s.rend());
        // string a= reverse(s.begin(),s.end());
        // int m= a.size();
        return LCS(s,reversed);

        
    }
};
16800