Skip to content
This repository has been archived by the owner on Mar 2, 2024. It is now read-only.

Commit

Permalink
add: v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gustaveWPM committed May 21, 2023
1 parent 8fe9d20 commit b79675c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[https://en.wikipedia.org/wiki/Damerau–Levenshtein_distance](https://en.wikipedia.org/wiki/Damerau–Levenshtein_distance)
29 changes: 29 additions & 0 deletions damerauLevenshtein.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export function damerauLevenshtein(s1: string, s2: string): number {
const s1len = s1.length;
const s2len = s2.length;
const matrix: number[][] = Array.from({ length: s1len + 1 }, () => new Array(s2len + 1).fill(0));

for (let i: number = 1; i <= s1len; i++) {
matrix[i][0] = i;
}

for (let j: number = 1; j <= s2len; j++) {
matrix[0][j] = j;
}

for (let i: number = 1; i <= s1len; i++) {
for (let j: number = 1; j <= s2len; j++) {
if (s1[i - 1] === s2[j - 1]) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = 1 + Math.min(matrix[i - 1][j], matrix[i][j - 1], matrix[i - 1][j - 1]);
if (i > 1 && j > 1 && s1[i - 1] === s2[j - 2] && s1[i - 2] === s2[j - 1]) {
matrix[i][j] = Math.min(matrix[i][j], matrix[i - 2][j - 2] + 1);
}
}
}
}
return matrix[s1len][s2len];
}

export default damerauLevenshtein;

0 comments on commit b79675c

Please sign in to comment.