1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-11-30 03:55:40 +00:00
go-mtree/vendor/github.com/xrash/smetrics/hamming.go
Vincent Batts 9e437eee80
go: update modules
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2022-07-05 09:02:09 -04:00

25 lines
544 B
Go

package smetrics
import (
"fmt"
)
// The Hamming distance is the minimum number of substitutions required to change string A into string B. Both strings must have the same size. If the strings have different sizes, the function returns an error.
func Hamming(a, b string) (int, error) {
al := len(a)
bl := len(b)
if al != bl {
return -1, fmt.Errorf("strings are not equal (len(a)=%d, len(b)=%d)", al, bl)
}
var difference = 0
for i := range a {
if a[i] != b[i] {
difference = difference + 1
}
}
return difference, nil
}