The Merge Strings Alternately problem is a simple yet elegant string manipulation task often featured in beginner-level coding challenges and interview warmups. Given two input strings, word1 and word2, your goal is to merge them character by character in alternating order starting with the first character of word1, followed by the first character of word2, and continuing this pattern.

If the strings are of unequal lengths, the extra characters from the longer string should simply be appended to the end of the merged result.
This problem helps build a strong foundation in string traversal, loop control, and handling edge cases like mismatched string lengths. In the following explanation, we’ll walk through a clean and efficient Python solution to the Merge Strings Alternately problem, ensuring a solid understanding of the logic and implementation.
Table of Contents
You are given two strings, word1 and word2. Your task is to merge the strings alternately, by appending characters from each string one by one, starting with the first character of word1 then word2 and so on.
- If one string is longer than the other, append the remaining characters of the longer string to the merged result.
Example 1:
Input: word1 = "abc", word2 = "pqr"
Output: "apbqcr"
Example 2:
Input: word1 = "ab", word2 = "pqrs"
Output: "apbqrs"
Constraints:
- 1 ≤
word1.length,word2.length≤ 100 word1andword2consist of lowercase English letters
Python Solution
class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
m = len(word1)
n = len(word2)
i, j = 0, 0
result = []
while i < m or j < n:
if i < m:
result += word1[i]
i += 1
if j < n:
result += word2[j]
j += 1
return "".join(result)
Step-by-Step Explanation
Step 1: Define the Function
class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
- A method
mergeAlternatelyis created under the classSolution. - It accepts two input strings:
word1andword2.
Step 2: Calculate Lengths
m = len(word1)
n = len(word2)
mstores the length ofword1nstores the length ofword2
These are used to control the iteration process over each string.
Step 3: Initialize Pointers and Result List
i, j = 0, 0
result = []
iandjare pointers for tracking the current index inword1andword2respectively.resultis a list used to build the merged string efficiently.
Step 4: Loop Until All Characters Are Used
while i < m or j < n:
- The loop runs as long as there are remaining characters in either
word1orword2.
Step 5: Append Character from word1 If Available
if i < m:
result += word1[i]
i += 1
- If the current index
iis within bounds, thei-thcharacter fromword1is added to the result. - Increment
ito move to the next character.
Step 6: Append Character from word2 If Available
if j < n:
result += word2[j]
j += 1
- Similarly, if
jis within bounds, thej-thcharacter fromword2is added. - Increment
j.
Step 7: Return Final String
return "".join(result)
- The list
resultis joined into a single string using"".join()and returned as the final merged string.
Time and Space Complexity
- Time Complexity:
O(m + n)
Since we traverse both strings once - Space Complexity:
O(m + n)
For storing the characters in the result list
Summary
The mergeAlternately function is a simple and efficient way to merge two strings character by character. It handles different string lengths and uses pointer-based iteration to alternate between characters from word1 and word2.
1 thought on “LeetCode75: Merge Strings Alternately”