add GetLongestCommonPrefixLength func
This commit is contained in:
parent
0d7890f6aa
commit
453e6f9851
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue