Skip to content

Instantly share code, notes, and snippets.

@jimregan
Created July 20, 2017 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimregan/24891acdcbc0a8566078ca2d7d14147b to your computer and use it in GitHub Desktop.
Save jimregan/24891acdcbc0a8566078ca2d7d14147b to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
my @in = qw(asfalt asfaltu asfalcie);
sub longest_suffix {
my $list = shift;
my $first = shift(@{$list});
for my $second(@in) {
my @a = split(//, $first);
my @b = split(//, $second);
my $len = ($#a < $#b) ? $#a : $#b;
my $common = '';
for (my $i = 0; $i <= $len; $i++) {
if($a[$i] eq $b[$i]) {
$common .= $a[$i];
}
}
$first = $common;
}
$first;
}
print longest_suffix(\@in);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment