Lecture 2 - Discussion and Practice Question
Lecture 2 - Discussion and Practice Question
https://leetcode.com/problems/shortest-common-supersequence/description/
https://leetcode.com/problems/longest-palindromic-subsequence/description/
https://leetcode.com/problems/edit-distance/description/
https://leetcode.com/problems/delete-operation-for-two-strings/description/
https://leetcode.com/problems/distinct-subsequences/description/
Lecture 2 -Discussion
LCS(Memoization)
class Solution {
public:
int m=text1.length();
int n=text2.length();
vector<vector<int>> l(m+1,vector<int>(n+1,-1));
return LCS(text1,text2,m,n,l);}
//cout<<m<<" "<<n<<endl;
if(l[m][n]==-1)
{
if(m==0 || n==0)
l[m][n]= 0;
else if(text1[m-1]==text2[n-1]){
l[m][n]=1+LCS(text1,text2,m-1,n-1,l);
else{
l[m][n]=max(LCS(text1,text2,m-1,n,l),LCS(text1,text2,m,n-1,l));
return l[m][n];
};
LCS (Tabulation)
class Solution {
public:
int m=text1.length();
int n=text2.length();
vector<vector<int>> l(m+1,vector<int>(n+1,0));
int i,j;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if(text1[i-1]==text2[j-1])
{
l[i][j]=1+l[i-1][j-1];
else
l[i][j]=max(l[i-1][j],l[i][j-1]);
return l[m][n];
};