<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 lpsR(string s,string t,int ind1,int ind2){
        if(ind1==0 || ind2==0){
            return 0;
        }
        if(s[ind1-1]==t[ind2-1]){
            return 1+lpsR(s,t,ind1-1,ind2-1);
        }
        return max(lpsR(s,t,ind1-1,ind2),lpsR(s,t,ind1,ind2-1));
    }
    int lpsM(string s,string t,int ind1,int ind2,vector<vector<int>> &dp){
        if(ind1==0 || ind2==0){
            return 0;
        }
        if(dp[ind1][ind2]!=-1){
            return dp[ind1][ind2];
        }
        if(s[ind1-1]==t[ind2-1]){
            return dp[ind1][ind2]=1+lpsM(s,t,ind1-1,ind2-1,dp);
        }
        return dp[ind1][ind2]=max(lpsM(s,t,ind1-1,ind2,dp),lpsM(s,t,ind1,ind2-1,dp));
    }
    int lpsT(string s,string t){
        vector<vector<int>> dp(s.size()+1,vector<int>(t.size()+1,0));
        for(int ind1=1;ind1<=s.size();ind1++){
            for(int ind2=1;ind2<=t.size();ind2++){
                if(s[ind1-1]==t[ind2-1]){
                    dp[ind1][ind2]=1+dp[ind1-1][ind2-1];
                }
                else{
                    dp[ind1][ind2]=max(dp[ind1-1][ind2],dp[ind1][ind2-1]);
                }
            }
        }
        return dp[s.size()][t.size()];
    }
    int lpsO(string s,string t){
        vector<int> dp(t.size()+1,0),temp(t.size()+1,0);
        for(int ind1=1;ind1<=s.size();ind1++){
            for(int ind2=1;ind2<=t.size();ind2++){
                if(s[ind1-1]==t[ind2-1]){
                    temp[ind2]=1+dp[ind2-1];
                }
                else{
                    temp[ind2]=max(dp[ind2],temp[ind2-1]);
                }
            }
            dp=temp;
        }
        return dp[t.size()];
    }
    int longestPalindromeSubseq(string s) {
        string t="";
        for(int i=0;i<s.size();i++){
            t=s[i]+t;
        }

        // return lpsR(s,t,s.size(),t.size());

        // vector<vector<int>> dp(s.size()+1,vector<int>(t.size()+1,-1));
        // return lpsM(s,t,s.size(),t.size(),dp);

        // return lpsT(s,t);
        
        return lpsO(s,t);
    }
};
18300