Cody

# Problem 2309. Calculate the Damerau-Levenshtein distance between two strings.

Solution 2036769

Submitted on 27 Nov 2019 by Jean-Marie Sainthillier
• Size: 105
• This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
assert(isequal(Damerau_Levenshtein('mom','dad'),3)); % 3 substitutions

2   Pass
assert(isequal(Damerau_Levenshtein('dogs','dog'),1)); % 1 deletion

3   Pass
assert(isequal(Damerau_Levenshtein('true','true'),0)); % identity

4   Pass
assert(isequal(Damerau_Levenshtein('true','false'),4)); % 1 insertion, 3 substitutions

5   Pass
assert(isequal(Damerau_Levenshtein('abc','ca'),2)); % 1 deletion, 1 transposition

6   Pass
assert(isequal(Damerau_Levenshtein('tee','tree'),1));

7   Pass
assert(isequal(Damerau_Levenshtein('email','mails'),2));

8   Pass
assert(isequal(Damerau_Levenshtein('profit','gifts'),5));

9   Pass
% symmetry rnd=@()char(randi([97 122],1,randi([4 10]))); for k=1:10 str1=rnd(); str2=rnd(); a=Damerau_Levenshtein(str1,str2); b=Damerau_Levenshtein(str2,str1); assert(isequal(a,b)) end

10   Pass
% trinagle inequality rnd=@()char(randi([97 122],1,randi([4 10]))); for k=1:50 str1=rnd(); str2=rnd(); str3=rnd(); a=Damerau_Levenshtein(str2,str3); b=Damerau_Levenshtein(str3,str1); c=Damerau_Levenshtein(str1,str2); assert(a+b>=c) end