RosettaCodeData/Task/Longest-common-subsequence/Perl/longest-common-subsequence.pl

15 lines
405 B
Perl
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
sub lcs {
my ($a, $b) = @_;
if (!length($a) || !length($b)) {
return "";
}
if (substr($a, 0, 1) eq substr($b, 0, 1)) {
return substr($a, 0, 1) . lcs(substr($a, 1), substr($b, 1));
}
my $c = lcs(substr($a, 1), $b) || "";
my $d = lcs($a, substr($b, 1)) || "";
return length($c) > length($d) ? $c : $d;
}
2013-04-10 21:29:02 -07:00
2016-12-05 22:15:40 +01:00
print lcs("thisisatest", "testing123testing") . "\n";