-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution482.go
More file actions
34 lines (31 loc) · 762 Bytes
/
solution482.go
File metadata and controls
34 lines (31 loc) · 762 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
package solution482
import (
"strings"
)
// ============================================================================
// 482. License Key Formatting
// URL: https://leetcode.com/problems/license-key-formatting/
// ============================================================================
func licenseKeyFormatting(s string, k int) string {
sl := make([]byte, 0, len(s))
upper := strings.ToUpper(s)
d := 0
for i := len(upper) - 1; i >= 0; i-- {
ch := byte(upper[i])
if ch != '-' {
sl = append(sl, ch)
d++
}
if d == k && i > 0 {
sl = append(sl, '-')
d = 0
}
}
for i, j := 0, len(sl)-1; i < j; i, j = i+1, j-1 {
sl[i], sl[j] = sl[j], sl[i]
}
if len(sl) > 1 && sl[0] == '-' {
return string(sl[1:])
}
return string(sl)
}