Back to Main Page

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

Problem Overview

You are given two strings, word1 and word2. The task is to create a new string by merging characters alternately from each input. You begin with the first character of word1, followed by the first character of word2, then the second from word1, and so on. This continues until all characters from both strings are used. If one string is longer than the other, the remaining characters from the longer string are appended at the end.

Intuition

The main idea is to traverse both strings at the same time and combine corresponding characters. This works easily if both strings are of the same length. But when one string is longer, you need to handle the remaining characters properly. Python's built-in zip function stops at the shortest input, so it doesn't help when lengths differ. Instead, itertools.zip_longest is a better choice. It lets you continue pairing characters even after one input ends, filling in the missing values with an empty string. This way, the merging process becomes uniform and safe.

Step-by-Step Approach

First, import the zip_longest function from Python’s itertools module. You then use it to pair each character from word1 and word2. If one string is shorter, zip_longest fills in the gap with an empty string so the loop can continue without errors. For every paired set of characters, concatenate them and build the merged result as a single combined string. The final result is simply the join of all such pairs.

Example Walkthrough

Consider the input strings word1 = "Hello" and word2 = "World". You begin by combining 'H' from word1 and 'W' from word2, then 'e' and 'o', then 'l' and 'r', and so on. The process continues until both words are exhausted. In this case, both strings are of equal length, so there is no need to deal with leftovers. The combined result is "HWeorlld", formed by merging the characters in alternating order.

Time and Space Complexity

The time complexity of this approach is O(max(m, n)), where m and n are the lengths of word1 and word2. This is because each character from both strings is processed once. The space complexity is also O(m + n) since the final merged string contains all characters from both inputs.

Conclusion

Merging two strings alternately is a fundamental exercise in string manipulation and parallel traversal. Using zip_longest provides a clear and concise way to handle uneven string lengths while preserving alternating behavior. It allows you to write an elegant and readable solution that works in all cases without special conditionals.

Get Personalized Support at AlgoMap Bootcamp 💡