Levenshtein Distance Calculator

Enter two strings to see how many single-character edits separate them.

Result

3
62.5%

How it is calculated

The strings are split into Unicode characters, so an accented letter counts as one character. A table is filled in where cell (i, j) holds the cheapest way to turn the first i characters of A into the first j characters of B: d(i, j) = min(d(i−1, j) + 1, d(i, j−1) + 1, d(i−1, j−1) + c), with c = 0 when the two characters match and c = 1 when they do not. The corner cell is the distance d. Similarity is (1 − d / max(|A|, |B|)) × 100, shown as a percentage with one decimal. The comparison is case-sensitive and spaces count as characters. Two empty strings give no result, and so do pairs where the product of the two lengths exceeds 1,000,000 characters.

Worked example

Compare saturday (8 letters) with sunday (6 letters). Delete the a and the t from saturday to get surday, then replace r with n to get sunday. That is 2 deletions and 1 substitution, so the distance is 3. The longer string has 8 characters, so similarity = (1 − 3 / 8) × 100 = 62.5 %.

Try this example →

Common mistakes

  • Expecting upper and lower case to match. Kitten and kitten are one edit apart because the capital K counts as a different character.
  • Leaving a trailing space in a pasted string. A space is a character, so abc followed by a space against abc has a distance of 1.
  • Reading similarity as a measure of meaning. It only compares characters in order, so two words that mean the same can score low.
  • Treating the distance as the number of differing positions. Insertions and deletions shift everything after them, so the distance can be much smaller than a position-by-position comparison suggests.
  • Pasting two long texts and expecting a figure. Once the two lengths multiplied pass one million, the tool gives no result, so compare shorter pieces.

About this calculator

The Levenshtein distance between two strings is the minimum number of single-character edits — insertions, deletions or substitutions — needed to turn one string into the other. It was introduced by the Russian mathematician Vladimir Levenshtein in 1965 and is computed with a dynamic-programming grid that builds up the cheapest edit path cell by cell, comparing every prefix of one string against every prefix of the other.

The metric shows up everywhere text needs to be compared imperfectly: spell checkers use it to suggest the closest real word to a typo, search engines and autocomplete use it for fuzzy matching against misspelled queries, bioinformatics tools use a close variant of it to align DNA and protein sequences, and plagiarism detectors and version-control diff tools use it to measure how similar two documents or lines of code really are.

Enter two strings and this calculator computes their Levenshtein distance and a similarity percentage, so you can see at a glance how close — or far — the two pieces of text are from each other.

Was this helpful?

You can pick up to 3 reactions.

Comments (0)

  • Be the first to comment.

Popular calculators

All Calculators