-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution2000.go
More file actions
36 lines (30 loc) · 862 Bytes
/
solution2000.go
File metadata and controls
36 lines (30 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package solution2000
// ============================================================================
// 2000. Reverse Prefix of Word
// URL: https://leetcode.com/problems/reverse-prefix-of-word/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/2000---Reverse-Prefix-of-Word
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_reversePrefix-24 84749766 16.08 ns/op 8 B/op 1 allocs/op
PASS
*/
import (
"strings"
)
func reversePrefix(word string, ch byte) string {
idx := strings.IndexByte(word, ch)
if idx != -1 {
rev := func(s []byte, n int) []byte {
for i, j := 0, n; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
return string(rev([]byte(word), idx))
}
return word
}