1768. Merge Strings Alternately - Explanation
Description
You are given two strings, word1 and word2. Construct a new string by merging them in alternating order, starting with word1 — take one character from word1, then one from word2, and repeat this process.
If one string is longer than the other, append the remaining characters from the longer string to the end of the merged result.
Return the final merged string.
Example 1:
Input: word1 = "abc", word2 = "xyz"
Output: "axbycz"Example 2:
Input: word1 = "ab", word2 = "abbxxc"
Output: "aabbbxxc"Constraints:
1 <= word1.length, word2.length <= 100word1andword2consist of lowercase English letters.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Strings - Understanding string indexing and character access
- Two Pointers - Using multiple indices to traverse data structures simultaneously
- StringBuilder / String Concatenation - Efficiently building strings in loops to avoid O(n^2) complexity
1. Two Pointers - I
Intuition
We want to interleave characters from both strings, taking one from each in turn. Using two pointers, we can walk through both strings simultaneously. While both strings have characters remaining, we append one from each. Once one string is exhausted, we append whatever remains from the other string.
Algorithm
- Initialize two pointers
iandjat0, and an empty result list. - While both
i < len(word1)andj < len(word2):- Append
word1[i]to the result, then incrementi. - Append
word2[j]to the result, then incrementj.
- Append
- Append any remaining characters from
word1(from indexito end). - Append any remaining characters from
word2(from indexjto end). - Return the joined result string.
Time & Space Complexity
- Time complexity:
- Space complexity: for the output string.
Where and are the lengths of the strings and respectively.
2. Two Pointers - II
Intuition
Instead of handling the remaining characters separately after the main loop, we can continue the loop as long as either string has characters left. In each iteration, we check if each pointer is still valid before appending. This approach handles unequal length strings naturally within a single loop.
Algorithm
- Initialize two pointers
iandjat0, and an empty result list. - While
i < norj < m(wherenandmare the lengths of the strings):- If
i < n, appendword1[i]and incrementi. - If
j < m, appendword2[j]and incrementj.
- If
- Return the joined result string.
Time & Space Complexity
- Time complexity:
- Space complexity: for the output string.
Where and are the lengths of the strings and respectively.
3. One Pointer
Intuition
Since we always process characters at the same index from both strings in each iteration, we can simplify to a single index variable. We iterate up to the length of the longer string, and for each index, we add the character from each string if that index is valid.
Algorithm
- Let
nandmbe the lengths ofword1andword2. - Initialize an empty result list.
- For each index
ifrom0tomax(n, m) - 1:- If
i < n, appendword1[i]to the result. - If
i < m, appendword2[i]to the result.
- If
- Return the joined result string.
Time & Space Complexity
- Time complexity:
- Space complexity: for the output string.
Where and are the lengths of the strings and respectively.
Common Pitfalls
Forgetting to Append the Remaining Characters
When one string is longer than the other, the remaining characters must be appended after the alternating portion is complete. Stopping the loop when the shorter string ends without handling the leftover characters produces an incomplete result.
Using String Concatenation in a Loop
In many languages, repeatedly concatenating strings with + inside a loop creates a new string object each time, leading to O(n^2) time complexity. Use a StringBuilder, list of characters, or similar efficient structure to build the result, then join at the end.
Sign in to join the discussion