Merge Strings Alternatively
Merge Strings Alternatively
You are given two strings word1 and word2. Merge the strings by adding letters in alternating
order, starting with word1. If a string is longer than the other, append the additional letters onto
the end of the merged string.
Example 1:
Example 2:
Example 3:
Constraints:
To solve this problem, you need to merge two strings by alternately adding characters
from each string. If one string is longer than the other, the remaining characters of the
longer string should be appended to the merged result.
4. After the loop, check if any characters are left in either string and append them to
the list.
Overview
Our task is to merge the strings by adding letters in alternating order, starting
with word1. If one string is longer than the other, the additional letters must be
appended to the end of the merged string.
Intuition
There are numerous ways in which we can combine the given strings. We've covered
a few of them in this article.
An intuitive method is to use two pointers to iterate over both strings. Assume we
have two pointers, i and j, with i pointing to the first letter of word1 and j pointing to
the first letter of word2. We also create an empty string results to store the outcome.
We continue iterating over the given strings until both are exhausted. We stop
appending letters from word1 when i reaches the end of word1, and we stop
appending letters from word2' when j reaches the end of word2.
Here's a visual representation of how the approach works in the second example
given in the problem description:
1/7
Algorithm
1. Create two variables, m and n, to store the length of word1 and word2.
2. Create an empty string variable result to store the result of merged words.
3. Create two pointers, i and j to point to indices of word1 and word2. We initialize
both of them to 0.
4. While i < m || j < n:
• If i < m, it means that we have not completely traversed word1. As a
result, we append word1[i] to result. We increment i to point to next index
of words.
• If j < n, it means that we have not completely traversed word2. As a
result, we append word2[j] to result. We increment j to point to next index
of words.
5. Return results.
It is important to note how we form the result string in the following codes:
- cpp: The strings are mutable in cpp, which means they can be changed. As a result,
we used the string variable and performed all operations on it. It takes constant time
to append a character to the string.
- java: The String class is immutable in java. So we used the mutable StringBuilder to
concatenate letters to result.
- python: Strings are immutable in python as well. As a result, we used the list result to
append letters and later joined the list with an empty string to return it as a string
object. The join operation takes linear time equal to the length of results to
merge results with empty string.
Implementation
Complexity Analysis
Here, mmm is the length of word1 and nnn is the length of word2.
Intuition
Let i be the pointer that we'll use. We begin with i = 0 and progress to the size of the
longer word between word1 and word2, i.e., till i = max(word1.length(), word2.length()) .
However, if i exceeds the length of any word, we don't have any letters to add from
that word, so we ignore it and continue adding the letter from the longer word.
Algorithm
1. Create two variables, m and n, to store the length of word1 and word2.
2. Create an empty string variable result to store the result of merged words.
3. Iterate over word1 and word2 using a loop running from i = 0 to i < max(m, n) and
keep incrementing i by 1 after each iteration:
• If i < m, it means that we have not completely traversed word1. As a
result, we append word1[i] to result.
• If i < n, it means that we have not completely traversed word2. As a
result, we append word2[i] to result.
4. Return results.
Implementation
Complexity Analysis
Here, mmm is the length of word1 and nnn is the length of word2.