MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / mergeAlternately

Function mergeAlternately

merge_strings_alternately_1768/solution.go:3–27  ·  view source on GitHub ↗
(word1 string, word2 string)

Source from the content-addressed store, hash-verified

1package merge_strings_alternately_1768
2
3func mergeAlternately(word1 string, word2 string) string {
4 var (
5 res string
6 i, j, k int
7 )
8
9 for i+j < len(word1)+len(word2) {
10 if i < len(word1) && k%2 == 0 {
11 res += string(word1[i])
12 i++
13 if j < len(word2) {
14 k++
15 }
16 }
17 if j < len(word2) && k%2 == 1 {
18 res += string(word2[j])
19 j++
20 if i < len(word1) {
21 k++
22 }
23 }
24 }
25
26 return res
27}

Callers 1

Test_mergeAlternatelyFunction · 0.85

Calls

no outgoing calls

Tested by 1

Test_mergeAlternatelyFunction · 0.68