I have found a recursive solution to find the largest common suffix of two strings. How can I convert this to a dynamic programming solution. Its difficult for me to conceptualize a bottom up solution since suffixes are easiest to compare from the ends of two strings. I have an attempted solution but it seems like its top down to me.
ATTEMPT
var LCSuffDyn = function(X,Y) {
longest_suffix = [''];
var largest_string, smallest_string;
if (X.length > Y.length) {
largest_string = X;
smallest_string = Y;
} else {
largest_string = Y;
smallest_string = X;
}
for (var k=1; k<smallest_string.length; k++) {
if (X[X.length-k] === Y[Y.length-k]) {
longest_suffix[k] = X[X.length-k]+longest_suffix[k-1];
}
else break;
}
return longest_suffix[longest_suffix.length-1];
};
console.log(LCSuffDyn('cbbbbbbbbbbajlbbbbbbbaba', 'cajkbbbbbbbbbjklbaba'));
RECURSIVE
var LCSuffRec = function(X,Y) {
return rec(X,Y, X.length, Y.length);
function rec(X, Y, m, n) {
if (X[m-1] === Y[n-1]) return rec(X, Y, m-1, n-1) + X[m-1];
else return '';
}
};