Skip to content
Decoder

Merge Strings Alternately

Easy

Problem

Given two strings word1 and word2, build a new string by taking letters alternately, starting with word1: first letter of word1, first letter of word2, second of word1, and so on. If one string is longer, append the rest of it to the end.

Example 1

Input
word1 = "abc", word2 = "pqr"
Output
"apbqcr"

Example 2

Input
word1 = "ab", word2 = "pqrs"
Output
"apbqrs"

Example 3

Input
word1 = "abcd", word2 = "pq"
Output
"apbqcd"

Constraints

  • 1 <= word1.length, word2.length <= 100
  • Both strings contain only lowercase English letters.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.