| 231 | } |
| 232 | |
| 233 | function getEditDistance(left: string, right: string): number { |
| 234 | let previousRow = Array.from({ length: right.length + 1 }, (_, index) => index); |
| 235 | |
| 236 | for (let leftIndex = 0; leftIndex < left.length; leftIndex++) { |
| 237 | const currentRow = [leftIndex + 1]; |
| 238 | |
| 239 | for (let rightIndex = 0; rightIndex < right.length; rightIndex++) { |
| 240 | const substitutionCost = left[leftIndex] === right[rightIndex] ? 0 : 1; |
| 241 | currentRow.push( |
| 242 | Math.min( |
| 243 | currentRow[rightIndex] + 1, |
| 244 | previousRow[rightIndex + 1] + 1, |
| 245 | previousRow[rightIndex] + substitutionCost |
| 246 | ) |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | previousRow = currentRow; |
| 251 | } |
| 252 | |
| 253 | return previousRow[right.length] ?? 0; |
| 254 | } |