Data Match Algorithms

Damerau-Levenshtein Distance: How It Works, Examples & Free Calculator

Damerau-Levenshtein Distance: How It Works, Examples & Free Calculator

Definition

Damerau-Levenshtein distance

Damerau-Levenshtein distance measures the minimum number of character insertions, deletions, substitutions and adjacent swaps needed to turn one string into another. With standard unit edit costs, identical strings have a distance of 0, and each edit costs 1.

Key Takeaways

  • Damerau-Levenshtein supports insertion, deletion, substitution and adjacent character swaps.
  • A neighbouring letter swap counts as one edit, compared with two in standard Levenshtein.
  • Full Damerau-Levenshtein and Optimal String Alignment (OSA) can produce different results.
  • Raw distance is an edit count; a similarity percentage requires a separate conversion.
  • Use the score alongside other identifying fields when assessing potential record matches.
FREE MATCHING TOOL

Damerau-Levenshtein Distance Calculator

Compare two values using insertions, deletions, substitutions and adjacent character transpositions.

Try an example
1 Damerau-Levenshtein distance
Moderate similarity

These values differ by 1 edit.

Damerau-Damerau-Levenshtein distance counts the minimum number of insertions, deletions, substitutions and adjacent transpositions required to transform one value into the other.

Similarity 75.0%
Longest value 4 chars
Difference 1 edit required
Remember: A low edit distance can indicate similar text, but whether two records are truly the same entity depends on context, field type, thresholds and other matching rules.
From edit distance to intelligent matching

WinPure AdaptiveMatch™ can combine Damerau-Levenshtein with other algorithms, thresholds and field-level logic to improve matching accuracy across real-world datasets.

What Is Damerau-Levenshtein Distance?

Damerau-Levenshtein distance measures the minimum number of edits needed to turn one string into another. With the standard cost of one per edit, it supports four operations: insertion, deletion, substitution and swapping two adjacent characters, known as a transposition.

That fourth operation distinguishes it from standard Levenshtein distance. For example, changing MARTHA to MARHTA requires one adjacent swap. Damerau-Levenshtein therefore returns a distance of 1, while standard Levenshtein returns 2.

A distance of 0 means the strings are identical. Lower distances indicate fewer edits, although the significance of an edit also depends on string length.

In data matching, the algorithm can help identify names and other text values affected by typing errors. It measures differences between strings; other fields should also contribute when deciding whether two records describe the same person or organisation.

How Does Damerau-Levenshtein Distance Work?

Damerau-Levenshtein compares two strings and finds the lowest-cost sequence of edits needed to transform one into the other. With standard unit costs, each insertion, deletion, substitution or adjacent transposition counts as one edit.

The algorithm typically uses dynamic programming, storing the results of smaller comparisons rather than repeatedly calculating them. It builds a matrix that tracks the minimum edit cost between portions of the two strings. Full Damerau-Levenshtein also keeps track of earlier character occurrences to account for transpositions correctly.

For example, changing FORM to FROM requires swapping the adjacent letters O and R. Damerau-Levenshtein counts this as one edit. Standard Levenshtein requires two edits because it does not include transposition as a separate operation.

The final result is an edit distance: 0 means identical strings, while larger values mean more edits are required. It is not a similarity percentage unless a separate normalisation formula is applied.

The full algorithm and the restricted Optimal String Alignment (OSA) variant can return different results when edits interact. We explain that distinction later in this guide.

The Four Edit Operations

Damerau-Levenshtein distance allows four types of edit. With standard unit costs, each operation adds 1 to the total distance.

OperationWhat it doesExampleDistance
InsertionAdds one character.JON → JOHN1
DeletionRemoves one character.JOHNN → JOHN1
SubstitutionReplaces one character with another.SMITH → SMYTH1
Adjacent transpositionSwaps two neighbouring characters.FORM → FROM1

The first three operations are also supported by standard Levenshtein distance. Damerau-Levenshtein adds adjacent transposition, allowing a reversed pair of neighbouring letters to count as a single edit.

This does not mean that any rearrangement counts as one edit. Moving a character across several positions or reversing the order of whole words may require multiple operations.

Damerau-Levenshtein Examples

The examples below show how Damerau-Levenshtein handles individual typing errors and combinations of edits. All comparisons use a cost of 1 per operation.

Original valueCompared valueChanges requiredDistance
SMITHSMITHNo changes0
JONJOHNInsert H1
SMITHSMYTHReplace I with Y1
MARTHAMARHTASwap the adjacent letters T and H1
SMIHTSMYTHSwap H and T, then replace I with Y2

Worked Example: SMIHT → SMYTH

Two edits transform the first value into the second:

  1. SMIHT → SMITH: swap the adjacent letters H and T.
  2. SMITH → SMYTH: replace I with Y.

The Damerau-Levenshtein distance is therefore 2. Standard Levenshtein returns 3 because the adjacent swap requires two edits, followed by the substitution.

An edit distance describes the difference between the text values. For example, JOHN and JON are one edit apart, but they could still belong to different people. Other identifying fields are needed to assess a record match.

Damerau-Levenshtein vs Levenshtein

The main difference is how the algorithms handle adjacent transpositions. Standard Levenshtein distance allows insertions, deletions and substitutions. Damerau-Levenshtein also allows two neighbouring characters to be swapped as one operation.

MARTHA to MARHTA comparison showing Levenshtein distance 2 and Damerau-Levenshtein distance 1.

Swapping the adjacent letters T and H requires two edits with standard Levenshtein, but only one with Damerau-Levenshtein.

FeatureLevenshteinDamerau-Levenshtein
Insertion, deletion and substitutionSupportedSupported
Adjacent transpositionRequires two editsCounts as one edit
MARTHA → MARHTA21
SMITH → SMYTH11
Standard outputMinimum edit countMinimum edit count

Damerau-Levenshtein is useful when neighbouring letters are frequently entered in the wrong order. Levenshtein remains useful when missing, additional or substituted characters are the main source of variation.

Recognising transpositions can bring genuine spelling errors closer together, but it can also bring distinct values closer together. Neither algorithm is universally better for record matching: test both against representative data and assess the results alongside other identifying fields.

Full Damerau-Levenshtein vs Optimal String Alignment (OSA)

Two algorithms are often described as “Damerau-Levenshtein”: the full, unrestricted algorithm and the restricted Optimal String Alignment (OSA) variant. Both support insertion, deletion, substitution and adjacent transposition, but they differ when edits interact.

OSA restricts a substring from being edited more than once. Full Damerau-Levenshtein allows interacting edit sequences, such as transposing two characters and then inserting another character between them. As a result, the full algorithm can return a lower distance.

FeatureFull Damerau-LevenshteinOSA
Four edit operationsSupportedSupported
Restrictions on repeated edits to a substringNo OSA restrictionA substring cannot be edited more than once
CA → ABC, with unit edit costs23
Triangle inequalitySatisfied with standard unit costsNot always satisfied

Why CA → ABC Produces Different Results

CA to ABC comparison showing full Damerau-Levenshtein distance 2 and Optimal String Alignment distance 3.

Full Damerau-Levenshtein transforms CA into ABC in two edits. OSA’s restrictions make its minimum distance three.

Full Damerau-Levenshtein can transform CA into ABC in two operations:

  1. CA → AC: transpose the adjacent characters.
  2. AC → ABC: insert B between them.

OSA does not allow this interacting edit sequence, so its minimum cost is 3. For a simple transposition such as MARTHA → MARHTA, both variants return 1.

When comparing calculators, libraries or matching tools, check which variant they implement. With unit costs and no input transformation, CA → ABC provides a useful diagnostic: a result of 2 is consistent with the full algorithm, while 3 is consistent with OSA. Different results may reflect the algorithm’s restrictions rather than an error in the calculation.

When Is Damerau-Levenshtein Distance Useful?

Damerau-Levenshtein is useful when text contains typing errors, especially neighbouring letters entered in the wrong order. It also handles missing, additional and substituted characters.

Common applications include:

  • Name matching: identifying spelling variations such as MARTHA and MARHTA.
  • Customer deduplication: finding candidate duplicate records when names contain small typing errors.
  • Record linkage: comparing text fields across systems where values have been entered inconsistently.
  • Search and spelling suggestions: finding values close to a mistyped query.

It is most helpful when character edits explain the differences between values. It does not directly recognise nicknames, pronunciation, abbreviations or the meaning of words. Phonetic algorithms can help with pronunciation-related variations, while token-based methods can help with reordered words. Nicknames and semantic relationships may require reference data or other matching methods.

For record matching, combine the text comparison with evidence from other fields. A similar name supported by an exact email address or consistent address details provides more context than an edit distance alone.

Limitations and False Matches

Damerau-Levenshtein measures character edits, so a low distance does not prove that two values identify the same person or organisation. JOHN and JOAN are only one edit apart, but they are distinct names.

  • String length matters: one edit changes a larger proportion of a short name than a long value. A fixed distance threshold can therefore behave differently across fields.
  • Meaning and pronunciation are not considered: nicknames, abbreviations and names that sound alike may remain several edits apart.
  • Word order can inflate the distance: JOHN SMITH and SMITH JOHN require multiple character edits, even though they contain the same words.
  • Data preparation affects results: capitalisation, punctuation, spaces and Unicode normalisation can change the distance unless handled consistently before comparison.
  • Implementation choices matter: full Damerau-Levenshtein and OSA can return different distances when edits interact. Edit costs and normalisation methods can also vary.

Allowing transpositions may recover genuine typing errors, but it can also bring unrelated values within a matching threshold. Test known matches and non-matches, then use other fields to assess whether similar text represents the same entity.

Choosing a Matching Threshold

A matching threshold determines how much variation a comparison allows. For raw Damerau-Levenshtein distance, lower values are stricter: a maximum distance of 1 accepts fewer pairs than a maximum distance of 2.

Some tools convert edit distance into a similarity score. One common normalisation, for standard unit edit costs, is:

Similarity = 1 − (distance ÷ length of the longer string)

For two empty strings, define similarity as 1 rather than dividing by zero.

For example, MARTHA and MARHTA have a distance of 1 and a maximum length of 6. Under this formula, their similarity is approximately 0.8333, or 83.33%. This is a normalised string score, not an 83.33% probability that the records describe the same person. Other tools may use a different conversion.

Choose a threshold using representative data:

  1. Confirm the measure: check the algorithm variant, edit costs and whether the tool reports distance or similarity.
  2. Test known matches and non-matches: include short names, common names and realistic typing errors.
  3. Compare the trade-offs: review which genuine matches are missed and which unrelated records are accepted as the threshold changes.
  4. Consider other fields: decide how the name comparison contributes alongside address, email or other identifying evidence.

There is no universal threshold. A distance of 1 may be useful for generating candidates, but its significance depends on the string length, the data and the consequences of a false match.

Using Damerau-Levenshtein in AdaptiveMatch™

AdaptiveMatch™ lets you apply different matching algorithms to individual columns in WinPure Clean & Match Enterprise. Damerau-Levenshtein can contribute where typing errors, including adjacent letter swaps, are a common source of variation.

A matching rule could combine:

  • Name: Damerau-Levenshtein to compare spelling differences and transposed characters.
  • Address: WinPureFuzzy™ to assess textual similarity.
  • Email: exact matching to check agreement on the email value.

AdaptiveMatch™ recommends configurations based on the type and characteristics of each column, while allowing users to review and adjust the algorithms, weights and thresholds. This places the name comparison within a wider matching rule, where evidence from other fields helps distinguish potential duplicates from unrelated records.

Turn spelling variations into potential matches

Compare names alongside addresses, emails and other identifying fields in WinPure Clean & Match Enterprise. Try it free for 30 days—no credit card required.

Start Free Trial

Frequently Asked Questions

Written by

Team WinPure

The WinPure Team brings together over 100 years of combined expertise in data quality, data management, data matching, entity resolution and software innovation. Our growing team of experienced specialists combines deep industry knowledge with a passion for advancing data quality technology, helping organisations worldwide improve data accuracy, identify and resolve duplicate records, streamline operations and unlock the full potential of their data.

Have a Data Quality Problem to Solve?

Talk to our team about your data, your requirements, and how WinPure could support your project.

Talk to Our Team

Get practical data quality guidance in your inbox

Receive our latest articles on data cleansing, matching, deduplication, entity resolution, and golden records.

Keep Reading

Start Your 30-Day Trial!

Secure desktop tool. No credit card required.

  • Full-feature access for 30 days
  • Runs on your own machine, data stays local
  • No credit card required
  • Onboarding support from our data team