AlgoMaster Logo

Merge Strings Alternately

easyFrequency4 min readUpdated June 23, 2026

Understanding the Problem

We have two strings and need to weave them together character by character, alternating between them. Take the first character from word1, then the first from word2, then the second from word1, then the second from word2, and so on.

The complication is that the strings might not be the same length. When one string runs out, we append whatever remains of the longer string. This is a zip that does not stop when the shorter input ends.

The core of the problem is iterating up to the length of the longer string and taking a character from each string at every index where one exists.

Key Constraints:

  • 1 <= word1.length, word2.length <= 100 → Both inputs are short, and the lengths bound the output: the merged string has exactly word1.length + word2.length characters. The natural solution is a single linear pass.
  • word1 and word2 consist of lowercase English letters → Both strings are non-empty, so there is always at least one character to place.

Approach 1: Two Pointers (Process Both, Then Append Remainder)

Intuition

Handle the merge in two phases. First, walk through both strings together for as long as both have characters, alternating one character from word1 and one from word2. Once the shorter string runs out, append whatever remains from the longer one.

This matches the plain description of the task: take turns picking from each string until one runs out, then append the rest.

Algorithm

  1. Initialize an empty result (using a StringBuilder or equivalent).
  2. Use a pointer i starting at 0.
  3. While i is less than the length of both word1 and word2:
    • Append word1[i] to the result.
    • Append word2[i] to the result.
    • Increment i.
  4. If word1 has remaining characters (from index i onward), append them all.
  5. If word2 has remaining characters (from index i onward), append them all.
  6. Return the result as a string.

Example Walkthrough

1Initialize: result is empty
1/6

Code

This is optimal in time and space. The three separate loops can collapse into one, which the next approach does.

Approach 2: Single Loop (Optimal and Cleaner)

Intuition

Instead of splitting the work into an interleave phase and a remainder phase, do it in one loop. Iterate from i = 0 up to the maximum of the two lengths. At each step, if word1 has a character at index i, add it; if word2 has a character at index i, add it.

This handles unequal lengths through the bounds checks alone. When one string runs out, its check fails at the remaining indices and the loop keeps adding from the other string, so no separate remainder logic is needed.

Algorithm

  1. Initialize an empty result (using a StringBuilder or equivalent).
  2. Find the maximum length: maxLen = max(len(word1), len(word2)).
  3. For i from 0 to maxLen - 1:
    • If i < len(word1), append word1[i].
    • If i < len(word2), append word2[i].
  4. Return the result as a string.

Example Walkthrough

1Initialize: result is empty
1/6

Code