<p>Given two strings <code>text1</code> and <code>text2</code>, return <em>the length of their longest <strong>common subsequence</strong>. </em>If there is no <strong>common subsequence</strong>, return <code>0</code>.</p>

<p>A <strong>subsequence</strong> of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.</p>

<ul>
	<li>For example, <code>&quot;ace&quot;</code> is a subsequence of <code>&quot;abcde&quot;</code>.</li>
</ul>

<p>A <strong>common subsequence</strong> of two strings is a subsequence that is common to both strings.</p>

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

<pre>
<strong>Input:</strong> text1 = &quot;abcde&quot;, text2 = &quot;ace&quot; 
<strong>Output:</strong> 3  
<strong>Explanation:</strong> The longest common subsequence is &quot;ace&quot; and its length is 3.
</pre>

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

<pre>
<strong>Input:</strong> text1 = &quot;abc&quot;, text2 = &quot;abc&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest common subsequence is &quot;abc&quot; and its length is 3.
</pre>

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

<pre>
<strong>Input:</strong> text1 = &quot;abc&quot;, text2 = &quot;def&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no such common subsequence, so the result is 0.
</pre>

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

<ul>
	<li><code>1 &lt;= text1.length, text2.length &lt;= 1000</code></li>
	<li><code>text1</code> and <code>text2</code> consist of only lowercase English characters.</li>
</ul>

// Longest Common Subsequence
// given two strings s1 and s2, find length of LCS
// start at end of both strings, if match 1 + [move, move]
// else 0 + max([move, stay], [stay, move])

// // Recursion
// // f(i,j) : length of LCS in s1[0,1,2,...i] and s2[0,1,2,...j]
// class Solution {
// public:
//     int longestCommonSubsequence(string s1, string s2) {
//         int m = s1.length();            // length of s1
//         int n = s2.length();            // length of s2
//         return f(m - 1, n - 1, s1, s2); // call a recursive function
//     }
//     int f(int i, int j, string& s1, string& s2) {
//         // base case
//         // if any string comes out of bound then return 0
//         if (i < 0 || j < 0)
//             return 0;
//         // if match occurs, count current char +1 and
//         // find lcs in s1[0,1,2,...i-1] and s2[0,1,2,...j-1]
//         if (s1[i] == s2[j])
//             return 1 + f(i - 1, j - 1, s1, s2);
//         // else two cases to cover all possibilities
//         // one [move, stay] second [stay, move] => return maxm of both
//         else
//             return 0 + max(f(i - 1, j, s1, s2), f(i, j - 1, s1, s2));
//     }
// };

// // Memoization
// // two parameters 1. i : 0 to m-1 [m values]  2. j : 0 to n-1 [n values]
// class Solution {
// public:
//     int longestCommonSubsequence(string s1, string s2) {
//         int m = s1.length();
//         int n = s2.length();
//         vector<vector<int>> dp(m, vector<int>(n, -1)); // dp martix
//         return f(m - 1, n - 1, s1, s2, dp);
//     }
//     int f(int i, int j, string& s1, string& s2, vector<vector<int>>& dp) {
//         if (i < 0 || j < 0)
//             return 0;
//         if (dp[i][j] != -1) // if already found
//             return dp[i][j];
//         if (s1[i] == s2[j])
//             // collect and return
//             return dp[i][j] = 1 + f(i - 1, j - 1, s1, s2, dp);
//         else
//             // collect and return
//             return dp[i][j] = 0 + max(f(i - 1, j, s1, s2, dp),
//                                       f(i, j - 1, s1, s2, dp));
//     }
// };

// Tabulation
// in base case we have i < 0 and j < 0 which we cannot represent -ve indexes in
// dp matrix, so right shift indexes by +1 updated memoization solution
// Memoization two parameters 1. i : 0 to m [m+1 values]  2. j : 0 to n [n+1
// values] here -1 0 1 2 3.........m-1 old memoization
//       0 1 2 3 4.....m-1 m   new memoization  lly for j
// two parameters 1. i : 0 to m-1 [m values]  2. j : 0 to n-1 [n values]
class Solution {
public:
    int longestCommonSubsequence(string s1, string s2) {
        int m = s1.length();
        int n = s2.length();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, -1)); // dp martix
        return f(m, n, s1, s2, dp);
    }
    int f(int i, int j, string& s1, string& s2, vector<vector<int>>& dp) {
        if (i == 0 || j == 0)
            return 0;
        if (dp[i][j] != -1) // if already found
            return dp[i][j];
        if (s1[i - 1] == s2[j - 1])
            // collect and return
            return dp[i][j] = 1 + f(i - 1, j - 1, s1, s2, dp);
        else
            // collect and return
            return dp[i][j] = 0 + max(f(i - 1, j, s1, s2, dp),
                                      f(i, j - 1, s1, s2, dp));
    }
};
24213