Skip to content

Instantly share code, notes, and snippets.

@AlexR1712
Created March 19, 2018 21:26
Show Gist options
  • Save AlexR1712/8cabaaf174b93b13ff7d37e84a88b80c to your computer and use it in GitHub Desktop.
Save AlexR1712/8cabaaf174b93b13ff7d37e84a88b80c to your computer and use it in GitHub Desktop.

Revisions

  1. AlexR1712 created this gist Mar 19, 2018.
    22 changes: 22 additions & 0 deletions levenshtein.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?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'));