Matching Strings
Problem
You want to match two or more strings.
Solution
Calculate the edit distance, or number of operations required to transform one string into the other.
Discussion
You can use either Hirschberg or Wagner–Fischer’s algorithm to calculate a Levenshtein distance. This example uses Wagner–Fischer’s algorithm.
This version of Levenshtein algorithm is linear in memory, quadratic in time.
str.charAt i is preferred here to str[i] because the latter syntax is not supported by some browsers (e.g. IE7).
At first glance the use of “by 1” in the two loops might look useless. It is actually here to avoid a common danger of the coffeescript [i..j] syntax. If str1 or str2 is an empty string, then [1..l1] or [1..l2] will return [1,0]. The loops with the “by 1” statement also compiles to cleaner / slightly more performant javascript.
Finally the optimization of recycling of arrays at the end of the loops is mainly here to demonstrate the syntax of coffeescript for swapping two variables.