Merge Strings Alternately

You are given two strings word1 and word2. Merge them by adding letters in alternating order, starting with word1. If one string is longer than the other, append the additional letters onto the end of the merged string. Return the merged string.

30:00

Merge Strings Alternately
easy
Free written solution
Topics
Companies

You are given two strings word1 and word2. Merge them by adding letters in alternating order, starting with word1. If one string is longer than the other, append the additional letters onto the end of the merged string. Return the merged string.

Example 1:
Input: {"word1":"ab","word2":"pqrs"}
Output: apbqrs
Constraints:
  • 1 <= word1.length, word2.length <= 100

  • word1 and word2 consist of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"word1":"ab","word2":"pqrs"}

One index i walks both words together. Each round takes word1[i] then word2[i], skipping a word once it has run out.

column i pairs word1[i] with word2[i]

word1 0/2

word2 0/4

0

a
p

1

b
q

2

r

3

s

out (0 of 6)

empty

Variables
VariableValue
i0
len(word1)2
len(word2)4
DepthFunction Call
Stack empty
0/8