Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.

A subsequence 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.


	For example, "ace" is a subsequence of "abcde".


A common subsequence of two strings is a subsequence that is common to both strings.

&nbsp;
Example 1:

Input: text1 = "abcde", text2 = "ace" 
Output: 3  
Explanation: The longest common subsequence is "ace" and its length is 3.


Example 2:

Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.


Example 3:

Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.


&nbsp;
Constraints:


	1 &lt;= text1.length, text2.length &lt;= 1000
	text1 and text2 consist of only lowercase English characters.


class Solution:
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        def lcs(ind1,ind2):
            prev=[0 for i in range(ind2+1)]
            curr=[0 for i in range(ind2+1)]
            
            for i in range(1,ind1+1):
                for j in range(1,ind2+1):
                    if text1[i-1]==text2[j-1]:
                        curr[j]=1+prev[j-1]
                         
                    else:
                        curr[j]=max(prev[j],curr[j-1])
                prev=list(curr) # remember to use a new list for prev

            return prev[-1]
                    
        
        ans=lcs(len(text1),len(text2))
        return ans
class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        int m = text1.length();
        int n = text2.length();
        int[][] dp = new int[m + 1][n + 1];

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
                    dp[i][j] = 1 + dp[i - 1][j - 1];
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }

        return dp[m][n];
    }
}
class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        int dp[1001][1001] = {0};
        for(int i = 1; i <= text1.size(); i++)
            for(int j = 1; j <= text2.size(); j++)
                if(text1[i-1] == text2[j-1]) dp[i][j] = 1 + dp[i-1][j-1];
                else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        return dp[text1.size()][text2.size()];
    }
};
/**
 * @param {string} text1
 * @param {string} text2
 * @return {number}
 */
let memo
const dp=(a,b,i,j)=>{
    if(i===0||j===0)return 0;
    if(memo[i][j]!=-1)return memo[i][j];
    if(a[i-1]===b[j-1]){
        return memo[i][j]= 1+ dp(a,b,i-1,j-1);
    }else{
        return memo[i][j]=Math.max(dp(a,b,i-1,j),dp(a,b,i,j-1));
    }

}
const bottomUp=(a,b)=>{

    for(let i=1;i<=a.length;i++){
        for(let j=1;j<=b.length;j++){
            if(a[i-1]===b[j-1]){
              memo[i][j]=1+memo[i-1][j-1]
            }else{
                memo[i][j]=Math.max(memo[i-1][j],memo[i][j-1]);
            }

        }
    }

return memo[a.length][b.length]

}

var longestCommonSubsequence = function(text1, text2) {
    memo=[];
    for(let i=0;i<=text1.length;i++){
        memo[i]=[];
        for(let j=0;j<=text2.length;j++){
          if(i===0||j===0)memo[i][j]=0;
            else memo[i][j]=-1;
        }
    }
    return bottomUp(text1,text2,text1.length,text2.length);
    // return dp(text1,text2,text1.length,text2.length);
};
Longest Common Subsequence