MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Distance

Function Distance

strings/hamming/hammingdistance.go:18–31  ·  view source on GitHub ↗
(str1, str2 string)

Source from the content-addressed store, hash-verified

16import "errors"
17
18func Distance(str1, str2 string) (int, error) {
19 if len(str1) != len(str2) {
20 return -1, errors.New("strings must have a same length")
21 }
22
23 hammingDistance := 0
24 for i := 0; i < len(str1); i++ {
25 if str1[i] != str2[i] {
26 hammingDistance++
27 }
28 }
29
30 return hammingDistance, nil
31}

Callers 1

TestHammingDistanceFunction · 0.70

Calls

no outgoing calls

Tested by 1

TestHammingDistanceFunction · 0.56