Calculate string distance using Damerau–Levenshtein method. See https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
This implementation is 1 to 1 with pseudo code from Wiki and it is fully correct as opposed to some performance optimized variants.