{"id":"6b093dddbd4755deb67ee067","slug":"edit-distance","title":"Edit distance (Levenshtein)","summary":"How many insertions, deletions and substitutions separate two strings? Dynamic programming on a 2D table, then saving memory down to a single row.","servedLang":"en","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"hard","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["dynamic-programming","strings","levenshtein"],"languages":[{"lang":"typescript","runner":"browser","starter":"/** Levenshtein distance: minimum number of single-character insertions, deletions and substitutions. */\nfunction editDistance(a: string, b: string): number {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":6,"tests":"function pattern(n, mul, add, square = false) {\n  let s = '';\n  for (let i = 0; i < n; i++) s += String.fromCharCode(97 + ((square ? i * i : 0) + i * mul + add) % 26);\n  return s;\n}\n\ntest('classic examples', () => {\n  expect(editDistance('kitten', 'sitting')).toBe(3);\n  expect(editDistance('flaw', 'lawn')).toBe(2);\n  expect(editDistance('intention', 'execution')).toBe(5);\n  expect(editDistance('sunday', 'saturday')).toBe(3);\n});\n\ntest('empty strings', () => {\n  expect(editDistance('', '')).toBe(0);\n  expect(editDistance('', 'abc')).toBe(3);\n  expect(editDistance('abcd', '')).toBe(4);\n});\n\ntest('identical and completely different strings', () => {\n  expect(editDistance('same', 'same')).toBe(0);\n  expect(editDistance('abc', 'xyz')).toBe(3);\n});\n\ntest('the distance is symmetric', () => {\n  expect(editDistance('sitting', 'kitten')).toBe(3);\n  expect(editDistance('lawn', 'flaw')).toBe(2);\n});\n\ntest('a single operation', () => {\n  expect(editDistance('comit', 'commit')).toBe(1);\n  expect(editDistance('commit', 'comit')).toBe(1);\n  expect(editDistance('commit', 'commot')).toBe(1);\n});\n\ntest('performance: 800 × 800 and 700 × 650 characters', () => {\n  expect(editDistance(pattern(800, 7, 0), pattern(800, 11, 3))).toBe(739);\n  expect(editDistance(pattern(700, 0, 0, true), pattern(650, 1, 0, true))).toBe(646);\n});\n"},{"lang":"javascript","runner":"browser","starter":"/**\n * Levenshtein distance: minimum number of single-character insertions, deletions and substitutions.\n * @param {string} a\n * @param {string} b\n * @returns {number}\n */\nfunction editDistance(a, b) {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":6,"tests":"function pattern(n, mul, add, square = false) {\n  let s = '';\n  for (let i = 0; i < n; i++) s += String.fromCharCode(97 + ((square ? i * i : 0) + i * mul + add) % 26);\n  return s;\n}\n\ntest('classic examples', () => {\n  expect(editDistance('kitten', 'sitting')).toBe(3);\n  expect(editDistance('flaw', 'lawn')).toBe(2);\n  expect(editDistance('intention', 'execution')).toBe(5);\n  expect(editDistance('sunday', 'saturday')).toBe(3);\n});\n\ntest('empty strings', () => {\n  expect(editDistance('', '')).toBe(0);\n  expect(editDistance('', 'abc')).toBe(3);\n  expect(editDistance('abcd', '')).toBe(4);\n});\n\ntest('identical and completely different strings', () => {\n  expect(editDistance('same', 'same')).toBe(0);\n  expect(editDistance('abc', 'xyz')).toBe(3);\n});\n\ntest('the distance is symmetric', () => {\n  expect(editDistance('sitting', 'kitten')).toBe(3);\n  expect(editDistance('lawn', 'flaw')).toBe(2);\n});\n\ntest('a single operation', () => {\n  expect(editDistance('comit', 'commit')).toBe(1);\n  expect(editDistance('commit', 'comit')).toBe(1);\n  expect(editDistance('commit', 'commot')).toBe(1);\n});\n\ntest('performance: 800 × 800 and 700 × 650 characters', () => {\n  expect(editDistance(pattern(800, 7, 0), pattern(800, 11, 3))).toBe(739);\n  expect(editDistance(pattern(700, 0, 0, true), pattern(650, 1, 0, true))).toBe(646);\n});\n"},{"lang":"python","runner":"browser","starter":"def edit_distance(a: str, b: str) -> int:\n    \"\"\"Levenshtein distance: minimum number of single-character insertions, deletions and substitutions.\"\"\"\n    raise NotImplementedError\n","testCount":6,"tests":"def pattern(n, mul, add, square=False):\n    return \"\".join(chr(97 + ((i * i if square else 0) + i * mul + add) % 26) for i in range(n))\n\n\ndef check(a, b, expected):\n    got = edit_distance(a, b)\n    assert got == expected, f\"edit_distance({a[:20]!r}, {b[:20]!r}): expected {expected}, got {got}\"\n\n\ndef test_classic_examples():\n    check(\"kitten\", \"sitting\", 3)\n    check(\"flaw\", \"lawn\", 2)\n    check(\"intention\", \"execution\", 5)\n    check(\"sunday\", \"saturday\", 3)\n\n\ndef test_empty_strings():\n    check(\"\", \"\", 0)\n    check(\"\", \"abc\", 3)\n    check(\"abcd\", \"\", 4)\n\n\ndef test_identical_and_completely_different():\n    check(\"same\", \"same\", 0)\n    check(\"abc\", \"xyz\", 3)\n\n\ndef test_distance_is_symmetric():\n    check(\"sitting\", \"kitten\", 3)\n    check(\"lawn\", \"flaw\", 2)\n\n\ndef test_single_operation():\n    check(\"comit\", \"commit\", 1)\n    check(\"commit\", \"comit\", 1)\n    check(\"commit\", \"commot\", 1)\n\n\ndef test_performance_800x800_and_700x650():\n    check(pattern(800, 7, 0), pattern(800, 11, 3), 739)\n    check(pattern(700, 0, 0, True), pattern(650, 1, 0, True), 646)\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\n\npublic static class Solution\n{\n    /// <summary>Levenshtein distance: minimum number of single-character insertions, deletions and substitutions.</summary>\n    public static int EditDistance(string a, string b)\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":6},{"lang":"java","runner":"judge0","starter":"class Solution {\n    /** Levenshtein distance: minimum number of single-character insertions, deletions and substitutions. */\n    static int editDistance(String a, String b) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":6},{"lang":"go","runner":"judge0","starter":"package main\n\n// EditDistance returns the Levenshtein distance: the minimum number of single-character\n// insertions, deletions and substitutions.\nfunc EditDistance(a, b string) int {\n\tpanic(\"not implemented\")\n}\n","testCount":6}],"questionCount":null,"xp":40,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:53.875Z","requestedLang":"en","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A CLI suggests \"Did you mean "},{"type":"text","text":"commit","marks":[{"type":"code"}]},{"type":"text","text":"?\" when someone types "},{"type":"text","text":"comit","marks":[{"type":"code"}]},{"type":"text","text":". Such suggestions (and spell checkers, DNA sequence comparison or typo-squatting detection in package names) rely on the "},{"type":"text","text":"Levenshtein edit distance","marks":[{"type":"bold"}]},{"type":"text","text":": the smallest number of single-character operations that turn one string into another."}]},{"type":"heading","attrs":{"level":2,"id":"task"},"content":[{"type":"text","text":"Task"}]},{"type":"paragraph","content":[{"type":"text","text":"Return the edit distance between strings "},{"type":"text","text":"a","marks":[{"type":"code"}]},{"type":"text","text":" and "},{"type":"text","text":"b","marks":[{"type":"code"}]},{"type":"text","text":". Allowed operations, each costing 1:"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"inserting","marks":[{"type":"bold"}]},{"type":"text","text":" a character,"}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"deleting","marks":[{"type":"bold"}]},{"type":"text","text":" a character,"}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"substituting","marks":[{"type":"bold"}]},{"type":"text","text":" a character with another."}]}]}]},{"type":"paragraph","content":[{"type":"text","text":"Signatures: "},{"type":"text","text":"editDistance(a, b)","marks":[{"type":"code"}]},{"type":"text","text":" (JS/TS), "},{"type":"text","text":"edit_distance(a, b)","marks":[{"type":"code"}]},{"type":"text","text":" (Python), "},{"type":"text","text":"Solution.EditDistance","marks":[{"type":"code"}]},{"type":"text","text":" (C#), "},{"type":"text","text":"Solution.editDistance","marks":[{"type":"code"}]},{"type":"text","text":" (Java), "},{"type":"text","text":"EditDistance","marks":[{"type":"code"}]},{"type":"text","text":" (Go)."}]},{"type":"heading","attrs":{"level":2,"id":"examples"},"content":[{"type":"text","text":"Examples"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"a","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"b","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Result"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Operations"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"kitten\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"sitting\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"3","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"k→s, e→i, +g"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"flaw\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"lawn\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"2","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"−f, +n"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"intention\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"execution\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"5","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph"}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"abc\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"3","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"three insertions"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"same\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"same\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph"}]}]}]},{"type":"heading","attrs":{"level":2,"id":"constraints"},"content":[{"type":"text","text":"Constraints"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"0 ≤ a.length, b.length ≤ 1,000","marks":[{"type":"code"}]},{"type":"text","text":", ASCII characters."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"expected-complexity"},"content":[{"type":"text","text":"Expected complexity"}]},{"type":"paragraph","content":[{"type":"text","text":"O(m · n)","marks":[{"type":"bold"}]},{"type":"text","text":" time, "},{"type":"text","text":"O(min(m, n))","marks":[{"type":"bold"}]},{"type":"text","text":" memory (the two-row version; the full O(m · n) table passes the tests too)."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/edit-distance/","title":"LeetCode 72 – Edit Distance","difficulty":null}],"articles":[],"topicsInfo":[],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:53.877Z","updatedAt":"2026-11-27T08:22:53.877Z"}