LeetCode Problem Workspace

Merge Strings Alternately

Merge two strings alternately, starting with the first string, and append extra characters when one string is longer.

category

2

Topics

code_blocks

7

Code langs

hub

3

Related

Practice Focus

Easy · Two-pointer scanning with invariant tracking

bolt

Answer-first summary

Merge two strings alternately, starting with the first string, and append extra characters when one string is longer.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Two-pointer scanning with invariant tracking

Try AiBox Copilotarrow_forward

To solve this problem, use two pointers to scan through the two strings. Merge the strings alternately by adding characters from each string one by one. If one string is longer, append the remaining characters from the longer string at the end of the merged result.

Problem Statement

You are given two strings, word1 and word2. Merge the strings by adding letters from each string alternately, starting with word1. If one string is longer than the other, append the remaining characters of the longer string to the end of the merged result.

Return the merged string as the result. You must handle strings of different lengths and merge them correctly, following the alternating pattern for as long as possible.

Examples

Example 1

Input: word1 = "abc", word2 = "pqr"

Output: "apbqcr"

The merged string will be merged as so:

word1: a b c

word2: p q r

merged: a p b q c r

Example 2

Input: word1 = "ab", word2 = "pqrs"

Output: "apbqrs"

Notice that as word2 is longer, "rs" is appended to the end.

word1: a b

word2: p q r s

merged: a p b q r s

Example 3

Input: word1 = "abcd", word2 = "pq"

Output: "apbqcd"

Notice that as word1 is longer, "cd" is appended to the end.

word1: a b c d

word2: p q

merged: a p b q c d

Constraints

  • 1 <= word1.length, word2.length <= 100
  • word1 and word2 consist of lowercase English letters.

Solution Approach

Two Pointer Approach

Use two pointers to iterate over the strings, alternating between the two by appending characters from each string. Start with word1, and then move both pointers forward to the next character in each string.

Handling Uneven Lengths

Once one string is fully processed, append the remaining characters from the longer string. This ensures the entire input is considered, even when the strings are of different lengths.

Optimized Space Complexity

The solution can be implemented with O(1) extra space, other than the space required for the result string, by modifying the result string directly.

Complexity Analysis

Metric Value
Time O(m + n)
Space O(1)

The time complexity is O(m + n), where m and n are the lengths of word1 and word2. The space complexity is O(1), as no extra space is required apart from the result string.

What Interviewers Usually Probe

  • Candidate understands the two-pointer technique and its application to string manipulation.
  • Candidate can handle edge cases when strings are of different lengths.
  • Candidate implements the solution with optimal space complexity.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to handle cases where one string is longer than the other.
  • Misusing string indexing, causing out-of-bound errors.
  • Not considering the O(1) space requirement and using extra space unnecessarily.

Follow-up variants

  • Handle strings with different character sets (e.g., uppercase letters).
  • Merge strings with varying characters, such as digits or special symbols.
  • Extend the pattern to merge more than two strings.

FAQ

How do I solve the Merge Strings Alternately problem?

Use two pointers to alternately merge characters from each string. When one string is exhausted, append the remaining characters from the longer string.

What is the time complexity of the Merge Strings Alternately problem?

The time complexity is O(m + n), where m and n are the lengths of the two strings.

Can I use more space to solve the Merge Strings Alternately problem?

While you can use extra space, the problem can be solved with O(1) extra space if you directly modify the result string.

What happens if the strings are of different lengths in the Merge Strings Alternately problem?

You append the remaining characters of the longer string after alternating characters from both strings.

How does the two-pointer technique help in the Merge Strings Alternately problem?

The two-pointer technique allows you to efficiently traverse both strings and merge them alternately in linear time.

terminal

Solution

Solution 1: Direct Simulation

We traverse the two strings `word1` and `word2`, take out the characters one by one, and append them to the result string. The Python code can be simplified into one line.

1
2
3
class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        return ''.join(a + b for a, b in zip_longest(word1, word2, fillvalue=''))
Merge Strings Alternately Solution: Two-pointer scanning with invariant t… | LeetCode #1768 Easy