Skip to content

Commit d2d18a7

Browse files
committed
exstrings: add LongestSequenceOf
1 parent 1de030f commit d2d18a7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

exstrings/stringutil.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package exstrings
99
import (
1010
"crypto/sha256"
1111
"crypto/subtle"
12+
"strings"
1213
"unsafe"
1314
)
1415

@@ -30,3 +31,24 @@ func SHA256(str string) [32]byte {
3031
func ConstantTimeEqual(a, b string) bool {
3132
return subtle.ConstantTimeCompare(UnsafeBytes(a), UnsafeBytes(b)) == 1
3233
}
34+
35+
func LongestSequenceOf(a string, b rune) int {
36+
// IndexRune has some optimizations, so use it to find the starting point
37+
firstIndex := strings.IndexRune(a, b)
38+
if firstIndex == -1 {
39+
return 0
40+
}
41+
count := 0
42+
maxCount := 0
43+
for _, r := range a[firstIndex:] {
44+
if r == b {
45+
count++
46+
if count > maxCount {
47+
maxCount = count
48+
}
49+
} else {
50+
count = 0
51+
}
52+
}
53+
return maxCount
54+
}

0 commit comments

Comments
 (0)