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

Solution 2905393

Submitted on 3 Sep 2020 by Rafael S.T. Vieira
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

7   Pass

8   Pass

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

Suggested Problems

More from this Author41

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!