Merge Strings Alternately
Difficulty: Easy
Problem
Given two strings word1 and word2, build a new string by taking one character from word1, then one from word2, and so on, alternating. When one string runs out, add whatever remains of the other to the end. Return the result.
Example
Input: word1 = "abc", word2 = "pqr"
Output: "apbqcr"
Explanation: Taking turns gives a, p, b, q, c, r, which spells "apbqcr".
Brute-force approach
Use two separate loops: first interleave characters up to the shorter string's length, then append the remainder of the longer string.
Steps
- Find the minimum length of both strings
- Interleave characters up to the minimum length
- Append remaining characters from the longer string
- Return the merged result
Time complexity: O(n + m) · Space complexity: O(n + m)
Trade-offs
- Uses two separate phases (interleave + append)
- Slightly more code than a single-loop approach
- String concatenation in a loop can be O((n+m)²) in some languages
Optimal approach
Key insight: A single index i can traverse both strings simultaneously. Just check bounds before accessing each string.
Use a single loop with one index variable. At each iteration, append from word1 if available, then from word2 if available. Handles unequal lengths naturally.
Steps
- Initialize result list and index i = 0
- Loop while i < len(word1) OR i < len(word2)
- Conditionally append word1[i] then word2[i]
- Increment i
- Return joined result
Time complexity: O(n + m) · Space complexity: O(n + m)
Pseudocode
function mergeAlternately(word1, word2):
result = []
i = 0
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)
Hints
- Use a single index to iterate through both strings
- Append from word1 first, then word2 at each index
- Continue until both strings are exhausted
Complexity questions
What is the time complexity of the single-loop merge approach?
O(n + m). We visit each character of word1 (n chars) and word2 (m chars) exactly once.
What is the space complexity of the merge approach?
O(n + m). The result string contains all n + m characters.
Why is the single-loop approach preferred over the two-phase approach?
It's cleaner code with fewer edge cases to handle. Same complexity, but the single loop eliminates the need for separate remainder-handling logic.
Check your understanding of Merge Strings Alternately
Given word1 = "ab" and word2 = "pqrs", what is the merged result?
"apbqrs". Alternate: a,p → b,q → then append remaining 'rs' → "apbqrs".
If word1 = "" (empty) and word2 = "hello", what is the output?
"hello". word1 has nothing to contribute, so the result is just word2 = "hello".
What determines when we stop alternating and start appending the remainder?
When we reach the end of the shorter string. Once the shorter string is exhausted, we just append remaining chars from the longer one.
Related problems
- Greatest Common Divisor of Strings (Easy)
- Reverse Vowels of a String (Easy)
- Reverse Words in a String (Medium)
- String Compression (Medium)