RosettaCodeData/Task/Longest-common-subsequence/JavaScript/longest-common-subsequence-2.js

11 lines
268 B
JavaScript
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
const longest = (xs, ys) => (xs.length > ys.length) ? xs : ys;
const lcs = (xx, yy) => {
if (!xx.length || !yy.length) { return ''; }
const [x, ...xs] = xx;
const [y, ...ys] = yy;
return (x === y) ? (x + lcs(xs, ys)) : longest(lcs(xx, ys), lcs(xs, yy));
};