Skip to content

Commit

Permalink
modified: Text/group_alike_words.pl
Browse files Browse the repository at this point in the history
  • Loading branch information
trizen committed Dec 12, 2016
1 parent ec5fa27 commit daa72e2
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions Text/group_alike_words.pl
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,21 @@ sub usage {
$opt{h} && usage();

# Levenshtein's distance function (optimized for speed)
sub lev {
sub leven {
my ($s, $t) = @_;

my @d = ([0 .. @$t], map { [$_] } 1 .. @$s);
foreach my $i (1 .. @$s) {
foreach my $j (1 .. @$t) {
$d[$i][$j] =
$$s[$i - 1] eq $$t[$j - 1]
? $d[$i - 1][$j - 1]
: min($d[$i - 1][$j], $d[$i][$j - 1], $d[$i - 1][$j - 1]) + 1;

foreach my $i (0 .. $#{$s}) {
foreach my $j (0 .. $#{$t}) {
$d[$i + 1][$j + 1] =
$s->[$i] eq $t->[$j]
? $d[$i][$j]
: 1 + min($d[$i][$j + 1], $d[$i + 1][$j], $d[$i][$j]);
}
}

$d[-1][-1] // min($#{$s} + 1, $#{$t} + 1);
$d[-1][-1];
}

# When no file has been provided, throw an error
Expand Down

0 comments on commit daa72e2

Please sign in to comment.