Files never leave your device

Levenshtein edit distance calculator

Count the smallest number of insertions, deletions and substitutions between two strings. Your inputs stay on your device.

Set up your task

Ready. Processing stays in this browser.

How to use Levenshtein edit distance calculator

Count the smallest number of insertions, deletions and substitutions between two strings. The pre-filled problem gives you a worked starting point. Compare the response with your own working before trying the self-check below.

  1. Check the example inputs: Original string, Target string.
  2. Change the inputs for your problem. Results update as you type, so finish entering the values before copying an answer.
  3. Use the working to check an intermediate value or assumption. Then change the example as suggested in the self-check and compare the result.

Supported inputs and limits

Use two strings of up to 80 Unicode code points each. Case and spaces count, and each edit costs one.

How it works

Levenshtein distance is the minimum number of single-character edits between two strings. Inserting adds a character, deleting removes one, and substituting replaces a character with another. A pair of equal characters can instead be matched at zero cost.

Dynamic programming compares the ends of two prefixes and chooses the cheapest of the possible edits. The row and column for an empty prefix show the cost of deleting or inserting every character. The trace lists one minimum set of edits. Separate marks in a Unicode sequence can count as separate code points.

Example

Run the pre-filled example. Its first result is “Levenshtein distance: 3”.

Original string
kitten
Target string
sitting
Levenshtein distance: 3
1. Replace k with s
2. Replace e with i
3. Insert g
Prefix-distance table
[ 0  1  2  3  4  5  6  7 ]
[ 1  1  2  3  4  5  6  7 ]
[ 2  2  1  2  3  4  5  6 ]

Questions about Levenshtein edit distance calculator

Are the operations applied to positions in the original string?

The trace lists operations in alignment order and describes the characters handled. It does not assign editable character offsets, which can move after insertions and deletions.

Is swapping two adjacent letters one edit?

This version uses Levenshtein operations. A transposition can be a single operation in the related Damerau–Levenshtein metric.

Compare an empty string with abc.

The distance is three because each character must be inserted. The first row of the prefix table runs from zero to three.

Related tools

Year 2: Algorithms and theory

Last updated