Skip to content

Instantly share code, notes, and snippets.

@lamdor
Created June 21, 2019 16:49
Show Gist options
  • Save lamdor/3f72dfd01c4d3f5f3062b585848dbbc4 to your computer and use it in GitHub Desktop.
Save lamdor/3f72dfd01c4d3f5f3062b585848dbbc4 to your computer and use it in GitHub Desktop.
package hamming
import (
"errors"
)
// Distance calculates the Hamming distance between two strings
func Distance(a, b string) (int, error) {
if len(a) != len(b) {
return 0, errors.New("Not same length")
}
var distance = 0
brunes := []rune(b)
for i, achr := range a {
bchr := brunes[i]
if achr != bchr {
distance++
}
}
return distance, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment