1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 23:22:28 +08:00

feat: merge some new functions from rc branch

This commit is contained in:
dudaodong
2024-03-04 16:36:20 +08:00
parent 48519aba2b
commit 928e3a390d
14 changed files with 1475 additions and 30 deletions

View File

@@ -5,6 +5,7 @@
package strutil
import (
"errors"
"regexp"
"strings"
"unicode"
@@ -537,3 +538,24 @@ func SubInBetween(str string, start string, end string) string {
return ""
}
// HammingDistance calculates the Hamming distance between two strings.
// The Hamming distance is the number of positions at which the corresponding symbols are different.
// This func returns an error if the input strings are of unequal lengths.
func HammingDistance(a, b string) (int, error) {
if len(a) != len(b) {
return -1, errors.New("a length and b length are unequal")
}
ar := []rune(a)
br := []rune(b)
var distance int
for i, codepoint := range ar {
if codepoint != br[i] {
distance++
}
}
return distance, nil
}