We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I tried to solve it is using c++ , this solve most test cases but not all not sure why.
int commonChild(string s1, string s2) {
// An example of longest common subsequence, not subsequence because the order of the characters matters here.
int m=s1.length();
int n=s2.length();
int L[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++)
{
L[i][j]=0;
}
}
// Following steps build L[m+1][n+1] in bottom up
// fashion. Note that L[i][j] contains length of LCS of
// X[0..i-1] and Y[0..j-1]
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (s1[i - 1] == s2[j - 1])
L[i][j] = max(max(L[i-1][j], L[i - 1][j - 1] + 1),L[i][j-1]);
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
// L[m][n] contains length of LCS for X[0..n-1]
// and Y[0..m-1]
return L[m][n];
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Common Child
You are viewing a single comment's thread. Return to all comments →
I tried to solve it is using c++ , this solve most test cases but not all not sure why.
int commonChild(string s1, string s2) { // An example of longest common subsequence, not subsequence because the order of the characters matters here. int m=s1.length(); int n=s2.length(); int L[m + 1][n + 1]; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { L[i][j]=0; } }
}