Skip to content

Instantly share code, notes, and snippets.

@AlexR1712
Created March 19, 2018 21:26
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 AlexR1712/8cabaaf174b93b13ff7d37e84a88b80c to your computer and use it in GitHub Desktop.
Save AlexR1712/8cabaaf174b93b13ff7d37e84a88b80c to your computer and use it in GitHub Desktop.
levenshtein. without native function
<?php
// Distancia Levenshtein en PHP (Sin usar la funciona nativa)
function lev($s,$t) {
$m = strlen($s);
$n = strlen($t);
for($i=0;$i<=$m;$i++) $d[$i][0] = $i;
for($j=0;$j<=$n;$j++) $d[0][$j] = $j;
for($i=1;$i<=$m;$i++) {
for($j=1;$j<=$n;$j++) {
$c = ($s[$i-1] == $t[$j-1])?0:1;
$d[$i][$j] = min($d[$i-1][$j]+1,$d[$i][$j-1]+1,$d[$i-1][$j-1]+$c);
}
}
return $d[$m][$n];
}
echo var_dump(lev('calle', 'calla'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment