Skip to content

Commit

Permalink
Merge pull request #69 from BlackTea1988/master
Browse files Browse the repository at this point in the history
fix bug
  • Loading branch information
peterhellberg committed Jan 30, 2013
2 parents 011ef15 + 19d514d commit 7437de8
Showing 1 changed file with 42 additions and 45 deletions.
87 changes: 42 additions & 45 deletions chapters/strings/matching-strings.md
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.

0 comments on commit 7437de8

Please sign in to comment.