Created
March 19, 2018 21:26
-
-
Save AlexR1712/8cabaaf174b93b13ff7d37e84a88b80c to your computer and use it in GitHub Desktop.
levenshtein. without native function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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