Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Merge Strings Alternately

Difficulty: Easy Source: NeetCode

Problem

You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a 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 = "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
  • word1 and word2 consist of lowercase English letters

Prerequisites

Before attempting this problem, you should be comfortable with:

  • Strings — building strings from characters, string concatenation
  • Two Pointers — advancing two indices through separate sequences in tandem

1. Brute Force (zip + remainder)

Intuition

Zip the two strings together so we process one character from each at a time. After the zip exhausts the shorter string, concatenate whatever is left from the longer string. This is idiomatic Python and perfectly readable, though some interviewers may want a more explicit pointer approach.

Algorithm

  1. Initialize result = [].
  2. Zip word1 and word2 — for each pair (c1, c2), append c1 then c2 to result.
  3. Find which string is longer and append its remaining characters.
  4. Return "".join(result).

Solution

def mergeAlternately(word1, word2):
    result = []
    for c1, c2 in zip(word1, word2):
        result.append(c1)
        result.append(c2)
    # Append the leftover from whichever string is longer
    m, n = len(word1), len(word2)
    if m > n:
        result.append(word1[n:])
    else:
        result.append(word2[m:])
    return "".join(result)


print(mergeAlternately("abc", "pqr"))   # apbqcr
print(mergeAlternately("ab", "pqrs"))   # apbqrs
print(mergeAlternately("abcd", "pq"))   # apbqcd

Complexity

  • Time: O(m + n)
  • Space: O(m + n)

2. Two Pointers

Intuition

Use a single pointer i that advances through both strings simultaneously. On each step, if i is within word1, take word1[i]; if i is within word2, take word2[i]. Keep going until i has passed the end of both strings. This makes the alternating logic and the remainder handling fall out naturally from a single loop.

Algorithm

  1. Initialize i = 0, result = [].
  2. While i < len(word1) or i < len(word2):
    • If i < len(word1), append word1[i].
    • If i < len(word2), append word2[i].
    • Increment i.
  3. Return "".join(result).

Solution

def mergeAlternately(word1, word2):
    i = 0
    result = []
    while i < len(word1) or i < len(word2):
        if i < len(word1):
            result.append(word1[i])
        if i < len(word2):
            result.append(word2[i])
        i += 1
    return "".join(result)


print(mergeAlternately("abc", "pqr"))   # apbqcr
print(mergeAlternately("ab", "pqrs"))   # apbqrs
print(mergeAlternately("abcd", "pq"))   # apbqcd

Complexity

  • Time: O(m + n)
  • Space: O(m + n)

Common Pitfalls

Using + to concatenate inside a loop. Strings are immutable in Python, so result += c creates a new string every iteration — that’s O(n²) time overall. Always collect characters into a list and join at the end.

Forgetting the remainder. zip stops at the shorter string. If you only use zip without handling the leftover, you’ll silently drop characters from the longer string.

Checking i < max(len(word1), len(word2)) instead of or. Using or in the while condition and guarding each append separately is the safest pattern — it avoids index-out-of-range errors without needing to compute the max length up front.