-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (53 loc) · 1.46 KB
/
main.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
)
// replace space with %20
// Input: "qwerty asdf zxcv ", Output: "qwerty%20asdf%20zxcv"
func main() {
str := "日の出qwerty adsf zcxv "
fmt.Printf("str <%v> with replaces spaces <%v>\n", str, replaceSpaces(str))
}
// cpu - O(N)
// mem - O(N) (we use temp rune slice, we can not convert slice (rune or byte) to string without allocation new space in Golang)
func replaceSpaces(s string) string {
charCntr := 0
spaceCntr := 0
spacesTailSize := 0
for _, ch := range s {
charCntr++
if ch == ' ' {
spaceCntr++
spacesTailSize++
} else {
// if not tail need set 0
spacesTailSize = 0
}
}
// each space (one char) we need repace with %20 (three chars)
// we already have rune for one char of each space so we need just add 2 more for each space
newSchars := make([]rune, len(s)+spaceCntr*2-spacesTailSize)
// we need index of char, we can not use i becuase it is index of bytes
charIdx := 0
// we need delta var to calculate index in newSchars because for each space we add two more chars
// for each space char we increase delta for two
delta := 0
// iterate from string but insert to rune slice
for i, ch := range s {
// if tail spaces break
if i > len(s)-spacesTailSize-1 {
break
}
charIdx++
if s[i] == ' ' {
newSchars[charIdx+delta] = '%'
delta++
newSchars[charIdx+delta] = '2'
delta++
newSchars[charIdx+delta] = '0'
} else {
newSchars[charIdx+delta] = ch
}
}
return string(newSchars)
}