question–>
code–>
class Solution {
public:
int lcs(string s1,string s2,int m,int n,int dp[][1001]){
if(m==0 || n==0){
return 0;
}
if(dp[m][n]!=-1){
return dp[m][n];
}
if(s1[m-1]==s2[n-1]){
return 1+lcs(s1,s2,m-1,n-1,dp);
}
else{
int op1=lcs(s1,s2,m,n-1,dp);
int op2=lcs(s1,s2,m-1,n,dp);
return dp[m][n]=max(op1,op2);}
}
int longestCommonSubsequence(string text1, string text2) {
int m=text1.length();
int n=text2.length();
int dp[1001][1001];
memset(dp,-1,sizeof(dp));
return lcs(text1,text2,m,n,dp);
}
};
showing tle topdown approach