-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path1.space20-solution.go
More file actions
41 lines (33 loc) · 844 Bytes
/
1.space20-solution.go
File metadata and controls
41 lines (33 loc) · 844 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
37
38
39
40
41
package main
import (
"fmt"
)
func space20(inputStr []byte) string {
spCount := 0
for _, v := range inputStr {
if string(v) == " " {
spCount++
}
}
resSize := len(inputStr) + 2*spCount - 1
inputStr = append(inputStr, make([]byte, 3*spCount)...)
for i := len(inputStr) - 3*spCount - 1; i >= 0; i-- {
if string(inputStr[i]) == " " {
inputStr[resSize] = []byte("0")[0]
inputStr[resSize-1] = []byte("2")[0]
inputStr[resSize-2] = []byte("%")[0]
resSize -= 2
} else {
inputStr[resSize] = inputStr[i]
}
resSize--
}
return string(inputStr)
}
func main() {
charArr := make([]byte, 0, 10000)
inputStr := " hello world, how are you ?"
charArr = append(charArr, inputStr...)
resultStr := space20(charArr)
fmt.Printf("Given string is: \t %s \nResultant string is: \t %s \n", string(inputStr), resultStr)
}