add GetLongestCommonPrefixLength func

This commit is contained in:
douxu 2025-09-27 15:56:46 +08:00
parent 0d7890f6aa
commit 453e6f9851
1 changed files with 16 additions and 0 deletions

View File

@ -152,3 +152,19 @@ func getConstantsKeyByLength(inputLen int) string {
return constants.RedisAllGridSetKey
}
}
// GetLongestCommonPrefixLength define func of get longest common prefix length between two strings
func GetLongestCommonPrefixLength(s1 string, s2 string) int {
// TODO 增加对特殊字符串例如 "" 的处理
minLen := len(s1)
if len(s2) < minLen {
minLen = len(s2)
}
for i := 0; i < minLen; i++ {
if s1[i] != s2[i] {
return i
}
}
return minLen
}