0% found this document useful (0 votes)
5 views

Lecture 2 - Discussion and Practice Question

Abcdej djfjfueei goffkrkwk rireigir eitififen r tiekefj

Uploaded by

aryanguptalko9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 2 - Discussion and Practice Question

Abcdej djfjfueei goffkrkwk rireigir eitififen r tiekefj

Uploaded by

aryanguptalko9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practice Questions

Printing of common subsequence

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 longestCommonSubsequence(string text1, string text2) {

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);}

int LCS(string &text1,string &text2,int m,int n,vector<vector<int>> &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 longestCommonSubsequence(string text1, string text2) {

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];

};

You might also like