Merge Strings Alternately - Leetcode Solution


💡 Step-by-Step Thought Process

  1. Start with two input strings, for example: "abc" and "xyz".
  2. Use a pointer for each string to keep track of where you are in the merging process.
  3. Alternate characters between the two strings: take one from the first string, then one from the second.
  4. Continue alternating until you reach the end of one or both strings.
  5. If one string is longer than the other, append the remaining characters from that string to the result.
  6. Return the final merged string.

Code Solution (Brute Force)


                

                

                

                

Code Solution (Optimal)


                

                

                

                

Detailed Explanation

How to Merge Two Strings Alternately

The merge-strings-alternately problem asks you to combine two input strings by alternating their characters. Specifically, you take the first character of the first string, then the first character of the second string, then the second character of the first string, and so on — continuing until all characters from both strings have been added. If one string is longer than the other, the remaining characters are added at the end of the merged result.

For example, if the input strings are word1 = "abc" and word2 = "pqr", the merged string is "apbqcr". Each letter from word1 and word2 is taken one at a time, alternating between the two:

  word1:   a   b   c
  word2:   p   q   r
  result:  a + p + b + q + c + r → "apbqcr"

If the input strings have unequal lengths, the algorithm still works. Consider word1 = "ab" and word2 = "pqrs". The merging process goes:

  word1:   a   b
  word2:   p   q   r   s
  result:  a + p + b + q + r + s → "apbqrs"

Approach for Solving

To solve the merge-strings-alternately problem, use a loop that iterates through both strings at the same time. You use two pointers (or indices): one for each string. On each iteration:

  • If the current index is within the bounds of word1, add its character to the result.
  • If the current index is within the bounds of word2, add its character to the result.
  • Advance the index and repeat until both strings are fully processed.

Pseudocode

result = ""
i = 0
while i < max(length of word1, length of word2):
    if i < length of word1:
        result += word1[i]
    if i < length of word2:
        result += word2[i]
    i += 1
return result

Time and Space Complexity

  • Time Complexity: O(n + m), where n is the length of word1 and m is the length of word2.
  • Space Complexity: O(n + m) to store the final merged string.

Summary

The merge two strings alternately technique is a basic string manipulation task that can be solved using a simple loop and two indices. It is a good exercise for understanding character-by-character traversal and handling of unequal-length strings. The solution is efficient, easy to implement, and useful for anyone learning to work with string inputs in coding challenges.

Get Personalized Support at AlgoMap Bootcamp 💡