Skip to content
uencoder

Levenshtein Distance

The Levenshtein distance is the minimum number of single-character insertions, deletions, and substitutions needed to make two strings identical.

Purpose

Measures the edit difference between strings, used for fuzzy matching and spelling correction.

Mechanism

Dynamic programming: d[i][j] = min(d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1]+cost), where cost is 0 if the characters match and 1 otherwise. Time and space are O(m×n); space can be optimized to O(min(m,n)).

Use cases

  • Spell checking and correction
  • DNA sequence alignment
  • Fuzzy search and deduplication
  • Natural language processing

Examples

  • Input
    kitten → sitting
    Output
    3

    Substitute k→s, e→i, and insert g

Common errors

Full O(mn) computation is too slow for long strings
Use a BK-tree or early termination by threshold

Related terms

Related tools

References