AlgoMaster Logo

Merge Strings Alternately

Last Updated: March 29, 2026

easy

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 twist is that the strings might not be the same length. When we run out of characters in the shorter string, we just tack on whatever is left of the longer string. So this isn't a strict "zip" operation. It's more like a zip that doesn't stop early.

The key observation: we're essentially iterating up to the length of the longer string, grabbing a character from each string when available.

Key Constraints:

  • 1 <= word1.length, word2.length <= 100 → Both strings are very short. Even an O(n * m) approach would only be 10,000 operations. Any reasonable approach will work.
  • word1 and word2 consist of lowercase English letters → No special characters, no empty strings to worry about.

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

Intuition

The first thing that comes to mind is to handle this in two phases. First, walk through both strings in lockstep for as long as both have characters. During this phase, we alternate: one character from word1, one from word2. Once the shorter string runs out, we just append whatever is left from the longer one.

This mirrors how you'd naturally describe the process out loud: "Take turns picking from each string until one runs out, then dump 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 approach is already optimal in time and space, but the three separate loops are a bit verbose. Can we handle everything in a single pass?

Approach 2: Single Loop (Optimal and Cleaner)

Intuition

Instead of splitting the work into "interleave phase" and "remainder phase," we can do it all in one loop. Iterate from i = 0 up to the maximum of the two lengths. At each step, if word1 still has a character at index i, add it. If word2 still has a character at index i, add it. That's it.

This naturally handles unequal lengths because the bounds check takes care of everything. When one string runs out, we just skip it and keep adding from the other. No special remainder logic 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