Levenshtein Distance Calculator
Count the insertions, deletions and substitutions needed to turn one string into another.
What it does
The Levenshtein distance counts the smallest number of single character edits needed to change one string into another. The allowed edits are inserting a character, deleting a character and substituting one character for another. Each edit costs one.
This tool compares two strings of letters and spaces, up to fifty characters each. It returns the edit distance, the two lengths and a simple similarity percentage based on the longer string.
The page opens with the classic example of kitten and sitting. Three edits are enough: substitute s for k, substitute i for e, then insert a final g. No two edit sequence can do better, so the distance is three.
Underneath, the tool fills a table where cell (i, j) is the distance between the first i characters of the first string and the first j characters of the second. The value comes from the smallest of a deletion, an insertion or a substitution plus the cost of the current characters.
Edit distance is used in spell checking, fuzzy search, DNA sequence comparison and record matching. It differs from Hamming distance, which only compares strings of equal length position by position.
How to use
- Type the first word or phrase.
- Type the second word or phrase using the same character set.
- Read the edit distance as the main result.
- Check the two lengths to understand the similarity percentage.
- Try identical strings to confirm a distance of zero.
- Switch the two strings to confirm that the distance is symmetric.
Formula
- D(i,j)
- The edit distance between the prefixes of length i and j.
- cost
- Zero when the two current characters match, otherwise one.
- similarity
- One minus distance divided by the longer length, expressed as a percent.
The three alternatives in the recurrence correspond to deleting a character from the first string, inserting a character from the second, or matching or substituting the two current characters. The full table has (m+1) times (n+1) cells, so the running time grows with the product of the two lengths.
Worked example
Example: kitten to sitting
The page opens with the strings kitten and sitting.
- The first string has 6 characters and the second has 7.
- Substitute s for k to get sitten, which is one edit.
- Substitute i for e to get sittin, which is the second edit.
- Insert g at the end to get sitting, which is the third edit.
- The dynamic programming table confirms that no sequence uses fewer than three edits.
- Similarity is 1 minus 3 divided by 7, about 57.1 percent.
- The tool reports distance 3, lengths 6 and 7, and similarity 57.1 %.
Reference table
| First string | Second string | Distance | Similarity |
|---|---|---|---|
| kitten | sitting | 3 | 57.1 % |
| flaw | lawn | 2 | 50.0 % |
| same | same | 0 | 100.0 % |
| uninformed | uniformed | 1 | 90.0 % |
Limits
- Accepts letters and spaces only, up to 50 characters per string.
- Transpositions of adjacent characters count as two edits, not one.
- The similarity percentage is a length based heuristic, not a statistical score.
- Case matters, since uppercase and lowercase letters are treated as different.
Common mistakes
- Treating a swapped pair of letters as a single edit.
- Using Hamming distance on strings of different lengths.
- Comparing characters without fixing the case convention.
- Reading the similarity percentage as a probability of being the same word.
- Counting only substitutions and ignoring insertions and deletions.
Try it yourself
Quick checks. Answers sit tucked under each question.
Distance between flaw and lawn?
2. Delete the leading f to get law, then insert n at the end to get lawn.
Distance between a word and itself?
0, and the length based similarity is 100.0 percent.
Is the distance from kitten to sitting the same as the reverse?
Yes. The distance is 3 in both directions.
Questions
What counts as one edit?
One insertion, one deletion or one substitution of a single character.
How is this different from Hamming distance?
Hamming distance compares equal length strings position by position and has no insertions or deletions.
Does the order of the strings matter?
No. The Levenshtein distance is symmetric.
How is the result computed?
A dynamic programming table builds distances between all pairs of prefixes and returns the final cell.
What does the similarity percentage mean?
It is one minus distance divided by the longer length. It is a quick heuristic, not a formal similarity measure.
Who introduced the measure?
Vladimir Levenshtein described it in 1965 in work on error correcting codes.
Sources
- Duke University, CPS 230 lecture 4 on dynamic programming and edit distance
- Tufts University, the Levenshtein distance and related distances
- University of Nevada Las Vegas, Levenshtein edit distance notes