EasyTwo PointersStringUpdated Sep 2026

1768. Merge Strings Alternately

Asked at Meta

Difficulty

Easy

Time

O(m + n)

Space

O(m + n)

Pattern

Two Pointers

Companies

1

Problem

You are given two strings, word1 and word2. Build a new string by taking characters from the two words one at a time, in turn, and always starting with word1.

So the first character comes from word1, the second from word2, the third from word1 again, and so on. When one word runs out of characters, just stick the rest of the other word onto the end of the result.

Return the merged string. Both words contain only lowercase English letters.

Take turns, starting with word1

word1abc012iword1 gives the 1st, 3rd and 5th characters of theanswer.word2pqr012jword2 gives the 2nd, 4th and 6th characters of theanswer.mergedapbqcr012345The result alternates orange (word1) and blue (word2)characters.
Characters are picked in turn: word1 first, then word2, and so on until one word is used up.

Constraints

  • ●1 <= word1.length, word2.length <= 100
  • ●word1 and word2 consist of lowercase English letters only

Examples

Example 1
Inputword1 = "abc", word2 = "pqr"
Output"apbqcr"
WhyBoth words have 3 letters, so we simply alternate all the way: a from word1, p from word2, b, q, c, r.
Example 2
Inputword1 = "ab", word2 = "pqrs"
Output"apbqrs"
WhyWe alternate a, p, b, q. Now word1 is empty, so the leftover "rs" from word2 is appended as-is.
Example 3
Inputword1 = "abcd", word2 = "pq"
Output"apbqcd"
WhyWe alternate a, p, b, q. Now word2 is empty, so the leftover "cd" from word1 is appended as-is.

Asked At

1 companies
CompanyDifficulty
MetaEasyView all Meta questions →

How to Think About It

1

Brute force: iterate through both strings simultaneously, appending one character from each at a time.

2

Use two pointers, one for each string, advancing them alternately.

3

Handle the case where one string is shorter by appending the suffix of the longer string.

4

Build the result using a list or StringBuilder for O(n) concatenation instead of repeated string concatenation.

5

Time O(m+n) where m and n are the lengths of the two strings, space O(m+n) for the result.

Optimal Approach

Initialize two pointers i and j at the start of each string. While both pointers are within bounds, append word1[i] then word2[j] to the result and increment both. After the loop, append the remaining characters from whichever string has leftover characters. Using a StringBuilder ensures O(m+n) time. Space is O(m+n) for the output string.

Two pointers, then copy the tail

word1ab01iStart: i = 0 on word1 and j = 0 on word2.word2pqrs0123jword2 is longer, so it will have letters left over.mergedleftoverapbqrs012345After i runs off word1, the rest of word2 ("rs") iscopied straight to the end.while i < len(word1) and j < len(word2): add word1[i], then word2[j]then add word1[i:] and word2[j:]
Walk both pointers together while both words still have letters, then dump the leftover part of the longer word.
ComplexityTime O(m + n)Space O(m + n)Each character of both words is copied into the result exactly once, and the result itself holds m + n characters.

What Trips People Up in Real Interviews

1

Clarify whether the merge starts with word1 or word2 (always word1).

2

Ask about empty strings as input (return the other string).

3

Mention using StringBuilder or list for efficiency instead of string concatenation.

4

Edge cases: equal length strings, one empty string, single character strings.

5

Walk through an example like "abc" + "pqr" producing "apbqcr".

Solution Code

O(m + n) time
def mergeAlternately(word1, word2):
    result = []
    i = j = 0
    while i < len(word1) and j < len(word2):
        result.append(word1[i])
        result.append(word2[j])
        i += 1
        j += 1
    result.append(word1[i:])
    result.append(word2[j:])
    return ''.join(result)

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Merge Strings Alternately problem?

Given two strings word1 and word2, merge them by alternating characters starting with word1. If one string is longer than the other, append the remaining characters at the end. This problem tests basic string manipulation and two-pointer traversal.

How do you solve Merge Strings Alternately?

The optimal approach runs in O(m + n) time and O(m + n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Merge Strings Alternately?

Merge Strings Alternately is asked at Meta. It is a easy difficulty problem.

What are common mistakes on Merge Strings Alternately?
  • Clarify whether the merge starts with word1 or word2 (always word1).
  • Ask about empty strings as input (return the other string).
  • Mention using StringBuilder or list for efficiency instead of string concatenation.
  • Edge cases: equal length strings, one empty string, single character strings.
  • Walk through an example like "abc" + "pqr" producing "apbqcr".