• + 0 comments

    Easiest ||C++||LCS

    int commonChild(string s1, string s2) {

    int n=s1.size();
    int m=s2.size();
    vector<int>pre(m+2),cur(m+2); //pre is previous row, cur is current row
    
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=m;j++)
        {
            if(i==0 or j==0)pre[j]=cur[j]=0;
    
            else if(s1[i-1]==s2[j-1]) cur[j]=pre[j-1]+1;
    
            else cur[j]=max(pre[j],cur[j-1]);
    
        }
        pre=cur;
    
    }
    
    return cur[m];
    

    }