(a, b)
| 12 | |
| 13 | // the minimum number of operations required to convert string a to string b. |
| 14 | function minDistance(a, b) { |
| 15 | const m = a.length |
| 16 | const n = b.length |
| 17 | |
| 18 | if (m < n) { |
| 19 | return minDistance(b, a) |
| 20 | } |
| 21 | |
| 22 | if (n === 0) { |
| 23 | return m |
| 24 | } |
| 25 | |
| 26 | let previousRow = Array.from({ length: n + 1 }, (_, i) => i) |
| 27 | |
| 28 | for (let i = 0; i < m; i++) { |
| 29 | const s1 = a[i] |
| 30 | let currentRow = [i + 1] |
| 31 | for (let j = 0; j < n; j++) { |
| 32 | const s2 = b[j] |
| 33 | const insertions = previousRow[j + 1] + 1 |
| 34 | const deletions = currentRow[j] + 1 |
| 35 | const substitutions = previousRow[j] + Number(s1 !== s2) |
| 36 | currentRow.push(Math.min(insertions, deletions, substitutions)) |
| 37 | } |
| 38 | previousRow = currentRow |
| 39 | } |
| 40 | return previousRow[previousRow.length - 1] |
| 41 | } |
| 42 | |
| 43 | export default defineRule({ |
| 44 | meta: { |
no test coverage detected