-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from BlackTea1988/master
fix bug
- Loading branch information
Showing
1 changed file
with
42 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,42 @@ | ||
--- | ||
layout: recipe | ||
title: Matching Strings | ||
chapter: 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. | ||
|
||
{% highlight coffeescript %} | ||
|
||
Levenshtein = | ||
(str1, str2) -> | ||
|
||
l1 = str1.length | ||
l2 = str2.length | ||
|
||
Math.max l1, l2 if Math.min l1, l2 == 0 | ||
|
||
i = 0; j = 0; distance = [] | ||
|
||
for i in [0...l1 + 1] | ||
distance[i] = [] | ||
distance[i][0] = i | ||
|
||
distance[0][j] = j for j in [0...l2 + 1] | ||
|
||
for i in [1...l1 + 1] | ||
for j in [1...l2 + 1] | ||
distance[i][j] = Math.min distance[i - 1][j] + 1, | ||
distance[i][j - 1] + 1, | ||
distance[i - 1][j - 1] + | ||
if (str1.charAt i - 1) == (str2.charAt j - 1) then 0 else 1 | ||
|
||
distance[l1][l2] | ||
|
||
{% endhighlight %} | ||
|
||
## Discussion | ||
|
||
You can use either Hirschberg or Wagner–Fischer's algorithm to calculate a Levenshtein distance. This example uses Wagner–Fischer's algorithm. | ||
--- | ||
layout: recipe | ||
title: Matching Strings | ||
chapter: 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. | ||
|
||
{% highlight coffeescript %} | ||
|
||
Levenshtein = | ||
(str1, str2) -> | ||
|
||
l1 = str1.length | ||
l2 = str2.length | ||
|
||
return Math.max l1, l2 unless l1 and l2 | ||
|
||
i = 0; j = 0; distance = [] | ||
|
||
distance[i] = [i] for i in [0..l1] | ||
distance[0][j] = j for j in [0..l2] | ||
|
||
for i in [1..l1] | ||
for j in [1..l2] | ||
distance[i][j] = Math.min distance[i - 1][j] + 1, | ||
distance[i][j - 1] + 1, | ||
distance[i - 1][j - 1] + | ||
if (str1.charAt i - 1) is (str2.charAt j - 1) then 0 else 1 | ||
|
||
distance[l1][l2] | ||
|
||
{% endhighlight %} | ||
|
||
## Discussion | ||
|
||
You can use either Hirschberg or Wagner–Fischer's algorithm to calculate a Levenshtein distance. This example uses Wagner–Fischer's algorithm. |