Skip to content

ML Models & Algorithms

Levenshtein Distance

In shortThe minimum number of single-character insertions, deletions or substitutions needed to turn one string into another.

Updated 24 Oct 2026· 2 min read

Levenshtein distance counts the smallest number of single-character edits (insertions, deletions or substitutions) needed to change one string into another. The lower the number, the more similar the strings. It is the classic measure behind distance-based fuzzy matching and the workhorse of many SEO clean-up scripts.

How it works

The textbook example is “kitten” to “sitting”, which takes three edits: substitute k with s, substitute e with i, and insert g at the end. Under the hood, the algorithm fills in a grid comparing every prefix of one string with every prefix of the other, which guarantees the minimum is found.

Raw distances are hard to compare across strings of different lengths, so most tools convert them into a similarity score, often 0 to 100. Be careful here: the popular ratio function in RapidFuzz and FuzzyWuzzy is based on insertions and deletions only (the Indel distance), so it can rank pairs differently from pure Levenshtein. RapidFuzz includes a dedicated Levenshtein module when you need the exact measure.

How it’s used in SEO and marketing

  • 404 mapping. Match broken URLs to the closest live URL.
  • Site migrations. Map old URLs to new ones in bulk; MLforSEO’s triple fuzzy matching notebook compares URLs, titles and headings together.
  • Keyword mapping. Assign keyword variants and misspellings to seed terms or topics.
  • Metadata comparison. Find titles that are almost identical to competitors’ or to each other.

Example

A crawl finds a 404 at /blog/keyword-reserch-guide. Comparing it with live URLs, /blog/keyword-research-guide is one insertion away (the missing “a”), so it scores far higher than any other candidate and becomes the redirect target. A quick human check confirms the match before the redirect goes live.

Limitations and things to watch

  • It has no idea of meaning. HARD and HAND are one edit apart, while HARD and HARDER are two, even though a person sees the second pair as more related.
  • Swapped letters count as two edits; the Damerau-Levenshtein variant counts them as one.
  • The cost of each comparison grows with the length of both strings, and comparing every pair in large lists adds up quickly, so narrow the candidates first.
  • Pick and validate thresholds on a sample you have checked by hand.

Learn it on MLforSEO

Where Levenshtein Distance shows up on MLforSEO

Sources and further reading

Other terms from ML Models & Algorithms

See all 55 ML Models & Algorithms terms →