<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 maxCommon(int ind1, int ind2, string& str1, string& str2){
//         if(ind1 < 0 || ind2 < 0)
//             return 0;

//         if(str1[ind1] == str2[ind2])
//             return 1 + maxCommon(ind1-1, ind2-1, str1, str2);
        
//         int pickL = maxCommon(ind1-1, ind2, str1, str2);
//         int pickR = maxCommon(ind1, ind2-1, str1, str2);
//         return max(pickL, pickR);
//     }
//     int longestPalindromeSubseq(string s) {
//         string text2 = s;
//         reverse(text2.begin(), text2.end());
        
//         if(s == text2)
//             return s.length();

//         int l1 = s.size(), l2 = text2.size();
//         return maxCommon(l1-1, l2-1, s, text2);
//     }   
// };








// class Solution {
// public:
//     int maxCommon(int ind1, int ind2, string& str1, string& str2, vector<vector<int>>& dp){
//         if(ind1 < 0 || ind2 < 0)
//             return 0;

//         if(dp[ind1][ind2] != -1)
//             return dp[ind1][ind2];

//         if(str1[ind1] == str2[ind2])
//             return dp[ind1][ind2] = 1 + maxCommon(ind1-1, ind2-1, str1, str2, dp);
        
//         int pickL = maxCommon(ind1-1, ind2, str1, str2, dp);
//         int pickR = maxCommon(ind1, ind2-1, str1, str2, dp);
//         return dp[ind1][ind2] = max(pickL, pickR);
//     }
//     int longestPalindromeSubseq(string text1) {
//         string text2 = text1;
//         reverse(text2.begin(), text2.end());
        
//         if(text1 == text2)
//             return text1.length();

//         int l1 = text1.size(), l2 = text2.size();
//         vector<vector<int>> dp(l1, vector<int>(l2, -1));

//         return maxCommon(l1-1, l2-1, text1, text2, dp);
//     }   
// };






//                          --SHIFTING OF INDEX --- MEMOIZATION
class Solution {
public:
    int maxCommon(int ind1, int ind2, string& str1, string& str2, vector<vector<int>>& dp){
        if(ind1 == 0 || ind2 == 0)
            return 0;

        if(dp[ind1][ind2] != -1)
            return dp[ind1][ind2];

        if(str1[ind1-1] == str2[ind2-1])
            return dp[ind1][ind2] = 1 + maxCommon(ind1-1, ind2-1, str1, str2, dp);
        
        int pickL = maxCommon(ind1-1, ind2, str1, str2, dp);
        int pickR = maxCommon(ind1, ind2-1, str1, str2, dp);
        return dp[ind1][ind2] = max(pickL, pickR);
    }
    int longestPalindromeSubseq(string text1) {
        string text2 = text1;
        reverse(text2.begin(), text2.end());
        
        if(text1 == text2)
            return text1.length();

        int l1 = text1.size(), l2 = text2.size();
        vector<vector<int>> dp(l1+1, vector<int>(l2+1, -1));

        return maxCommon(l1, l2, text1, text2, dp);
    }   
};






// class Solution {
// public:
//     int longestCommonSubsequence(string str1, string str2) {
//         if(str1 == str2)
//             return str1.length();

//         int l1 = str1.size(), l2 = str2.size();
//         vector<vector<int>> dp(l1+1, vector<int>(l2+1, 0));

//         for(int i = 0; i <= l1; i++)
//             dp[i][0] = 0;

//         for(int i = 0; i <= l2; i++)
//             dp[0][i] = 0;

//         for(int ind1 = 1; ind1 <= l1; ind1++){
//             for(int ind2 = 1; ind2 <= l2; ind2++){
//                 if(str1[ind1-1] == str2[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[l1][l2];
//     }   
// };



// class Solution {
// public:
//     int longestCommonSubsequence(string str1, string str2) {
//         if(str1 == str2)
//             return str1.length();

//         int l1 = str1.size(), l2 = str2.size();
//         vector<int>prev (l2+1, 0);
//         vector<int>cur (l2+1, 0);

//         for(int i = 0; i <= l2; i++)
//             prev[i] = 0;

//         for(int ind1 = 1; ind1 <= l1; ind1++){
//             for(int ind2 = 1; ind2 <= l2; ind2++){
//                 if(str1[ind1-1] == str2[ind2-1])
//                     cur[ind2] = 1 + prev[ind2-1];
//                 else
//                     cur[ind2] = max(prev[ind2], cur[ind2-1]);
//             }
//             prev = cur;
//         }
//         return prev[l2];
//     }   
// };
35000