Skip to content

Instantly share code, notes, and snippets.

@purohit
Forked from athiwatc/LevenshteinDistance.go
Last active December 28, 2015 15:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purohit/5194157 to your computer and use it in GitHub Desktop.
Save purohit/5194157 to your computer and use it in GitHub Desktop.
Updating this to work with Go v1
package LevenshteinDistance
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.Min(math.Min(float64(min1), float64(min2)), float64(min3)))
}
}
return d[len(a)][len(b)]
}
@iwegbue
Copy link

iwegbue commented Dec 28, 2015

Thanks for this. Any possibility of getting a python version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment