Problem 2309. Calculate the Damerau-Levenshtein distance between two strings.
Problem 2303 reminded me a few older ones dealing with metrics between strings, problems 93, 846 or 848 about Hamming and Levenshtein distances.
Damerau-Levenshtein distance is an extension to Levenshtein distance. It is also defined as minimum number of simple edit operations on string to change it into another, but the list of allowed operations is extended.
As it is written on Wikipedia there are 4 allowed edits: deletion, insertion and substitution of an single character and an transposition of two adjacent characters.
Example. Such defined distance between words gifts and profit is 5:
gifts => pgifts (insertion of 'p') pgifts => prgifts (insertion of 'r') prgifts => proifts (substitution of 'g' to 'o') proifts => profits (transposition of 'if' to 'fi') profits => profit (deletion of 's')
Solution Stats
Problem Comments
-
4 Comments
Test case #5 is correct, Asif. If you want, you can try it on wolfram alpha using DamerauLevenshteinDistance['abc','ca']. Be careful, because the algorithm from wikipedia has errors.
Another example that should return 2 via Damerou-Levenshtein and 3 via Levenshtein alone: 'jellyfish' and 'jellyifhs'.
The algorithm given by Wikipedia is correct, I think, but take note that the first algorithm Wikipedia gives does not actually compute the Damerau-Levenshtein distance, but rather a related metric. Confusing, but the entry says as much if you read it closely.
Solution Comments
Show commentsGroup

Algorithm I
- 17 Problems
- 5 Finishers
- Calculate the Levenshtein distance between two strings
- Calculate the Damerau-Levenshtein distance between two strings.
- Coin distribution
- Coin Distribution - 02
- Split bread like the Pharaohs - Egyptian fractions and greedy algorithm
- The Tortoise and the Hare - 01
- The Tortoise and the Hare - 02
- Quarantine Days
- Word Ladder
- Kolakoski Sequence
- Is this group simply connected?
- Sub-sequence - 01
- Sub-sequence - 02
- Sub-sequence - 03
- longest common substring : Skipped character version
- Find an optimal placement of coolers on a grid
- Chain multiplication - 02
Problem Recent Solvers27
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!