-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution1768.go
39 lines (33 loc) · 901 Bytes
/
solution1768.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
package solution1768
import "strings"
// ============================================================================
// 1768. Merge Strings Alternately
// URL: https://leetcode.com/problems/merge-strings-alternately/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/1768---Merge-Strings-Alternately
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_mergeAlternately-24 65478508 18.57 ns/op 8 B/op 1 allocs/op
PASS
*/
func mergeAlternately(word1 string, word2 string) string {
sb := strings.Builder{}
size1 := len(word1)
size2 := len(word2)
size := size1
if size2 > size1 {
size = size2
}
for i := 0; i < size; i++ {
if i < size1 {
sb.WriteByte(word1[i])
}
if i < size2 {
sb.WriteByte(word2[i])
}
}
return sb.String()
}