Skip to content

Instantly share code, notes, and snippets.

@athiwatc
Created July 7, 2011 12:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save athiwatc/1069374 to your computer and use it in GitHub Desktop.
Save athiwatc/1069374 to your computer and use it in GitHub Desktop.
LevenshteinDistance
package LevenshteinDistance
import "fmt"
import "math"
func compare(a, b string) int {
var cost int
d := make([][]int, len(a)+1)
for i := 0; i < len(d); i++ {
d[i] = make([]int, len(b)+1)
}
var min1, min2, min3 int
for i := 0; i < len(d); i++ {
d[i][0] = i
}
for i := 0; i < len(d[0]); i++ {
d[0][i] = i
}
for i := 1; i < len(d); i++ {
for j := 1; j < len(d[0]); j++ {
if a[i-1] == b[j-1] {
cost = 0
} else {
cost = 1
}
min1 = d[i-1][j] + 1
min2 = d[i][j-1] + 1
min3 = d[i-1][j-1] + cost
d[i][j] = int(math.Fmin(math.Fmin(float64(min1), float64(min2)), float64(min3)))
}
}
return d[len(a)][len(b)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment