RosettaCodeData/Task/Longest-common-subsequence/Sidef/longest-common-subsequence.sidef

17 lines
372 B
Text
Raw Permalink Normal View History

2016-12-05 23:44:36 +01:00
func lcs(xstr, ystr) is cached {
xstr.is_empty && return xstr;
ystr.is_empty && return ystr;
var(x, xs, y, ys) = (xstr.ft(0,0), xstr.ft(1),
ystr.ft(0,0), ystr.ft(1));
if (x == y) {
x + lcs(xs, ys)
} else {
[lcs(xstr, ys), lcs(xs, ystr)].max_by { .len };
}
}
say lcs("thisisatest", "testing123testing");