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
Constraints
- ●
1 <= word1.length, word2.length <= 100 - ●
word1 and word2 consist of lowercase English letters only
Examples
word1 = "abc", word2 = "pqr""apbqcr"word1 = "ab", word2 = "pqrs""apbqrs"word1 = "abcd", word2 = "pq""apbqcd"Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Meta | Easy | View all Meta questions → |
How to Think About It
Brute force: iterate through both strings simultaneously, appending one character from each at a time.
Use two pointers, one for each string, advancing them alternately.
Handle the case where one string is shorter by appending the suffix of the longer string.
Build the result using a list or StringBuilder for O(n) concatenation instead of repeated string concatenation.
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
What Trips People Up in Real Interviews
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".
Solution Code
O(m + n) timedef 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)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".