Skip to content

Instantly share code, notes, and snippets.

@luchaninov
Last active July 17, 2024 22:36
Show Gist options
  • Save luchaninov/a5730c453129ae159dfc to your computer and use it in GitHub Desktop.
Save luchaninov/a5730c453129ae159dfc to your computer and use it in GitHub Desktop.
Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one word into the other.
// from https://github.com/thinkphp/String.levenshtein/blob/master/Source/String.levenshtein.js but no depencencies
function levenshtein(str1, str2) {
var cost = new Array(),
n = str1.length,
m = str2.length,
i, j;
var minimum = function(a, b, c) {
var min = a;
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
return min;
}
if (n == 0) {
return;
}
if (m == 0) {
return;
}
for (var i = 0; i <= n; i++) {
cost[i] = new Array();
}
for (i = 0; i <= n; i++) {
cost[i][0] = i;
}
for (j = 0; j <= m; j++) {
cost[0][j] = j;
}
for (i = 1; i <= n; i++) {
var x = str1.charAt(i - 1);
for (j = 1; j <= m; j++) {
var y = str2.charAt(j - 1);
if (x == y) {
cost[i][j] = cost[i - 1][j - 1];
} else {
cost[i][j] = 1 + minimum(cost[i - 1][j - 1], cost[i][j - 1], cost[i - 1][j]);
}
} //endfor
} //endfor
return cost[n][m];
}
@devingfx
Copy link

Hi !
This code can optimized / shortened :

  • new Array() can be replaced by a [ ]
  • It exists a standard Math.min( a, b, c ) now
  • Semicollons (;) are useless if followed by a newline
  • Brackets ({ }) are useless if followed by a single statement
  • String.charAt is the same as bracket access: str1.charAt( i - 1 ) == str1[i - 1]
const levenshtein = ( str1, str2 )=> {
    var cost = [],
        n = str1.length,
        m = str2.length,
        i, j, x, y

    if( n == 0 || m == 0 ) return

    for( i = 0; i <= n; i++ )
        cost[i] = [i]

    for( j = 0; j <= m; j++ )
        cost[0][j] = j

    for( i = 1; i <= n; i++ )
    {
        var x = str1[i - 1]

        for( j = 1; j <= m; j++ )
        {
            var y = str2[j - 1]

            if (x == y)
                cost[i][j] = cost[i - 1][j - 1]
            else
                cost[i][j] = 1 + Math.min( cost[i - 1][j - 1], cost[i][j - 1], cost[i - 1][j] )

        }

    }

    return cost[n][m]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment