Skip to content

Instantly share code, notes, and snippets.

@raliste
Created December 19, 2010 20:49
Show Gist options
  • Save raliste/747675 to your computer and use it in GitHub Desktop.
Save raliste/747675 to your computer and use it in GitHub Desktop.
Levenshtein distance (substitution and insertion)
int cambios(string s1, string s2) {
int l1 = s1.size();
int l2 = s2.size();
int *A = new int[l2];
int i, j;
for(i=0; i<=l2; i++) {
A[i] = i;
}
for(i=0; i<l1; i++) {
A[0] = i+1;
int x = i;
for(j=0; j<l2; j++) {
int y = A[j+1];
if(s1[i] == s2[j])
A[j+1] = x;
else {
int n;
if(y < x)
n = y;
else
n = x;
if(A[j] < n)
A[j+1] = A[j];
else
A[j+1] = n + 1;
}
x = y;
}
}
return A[l2];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment