↓Skip to main content
  1. LeetCode/

LeetCode 1768: Merge Strings Alternately

·2 mins· ·
LeetCode Easy Two-Pointers String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-03-18

  • Submissions: Accepted → Time Limit Exceeded → Accepted

Approach
#

Starting with word1, take one character from each string in turn; once one runs out, append the rest of the other.

My Attempts
#

The first version used zip until the shorter string ran out, then computed how many characters had been used with round(len(res) / 2) and appended both remainders. Correct, but computing the length with round is roundabout.

The second version used a hand-written index:

idx = 0
while idx < len(word1) or idx < len(word2):
    res += word1[idx] + word2[idx]          # idx never increases

It forgot idx += 1, looping forever and timing out. And with or, it would also read past the end of the shorter string. Using and plus idx += 1 passed.

Solution
#

class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        res = []
        i = 0
        while i < len(word1) and i < len(word2):
            res.append(word1[i])
            res.append(word2[i])
            i += 1
        return "".join(res) + word1[i:] + word2[i:]

When the loop ends, at least one string is used up, so word1[i:] + word2[i:] appends only the one with characters left.

Complexity
#

Time O(n + m), space O(n + m).

Mistakes to Avoid
#

  • Forgetting to advance the index inside a while loop.
  • Using or in the condition: it reads past the end of the shorter string.
  • Concatenating with +=: Python strings are immutable, so each step builds a new string; collect into a list and join.

Interview Explanation
#

One index walks both strings, taking a character from each in turn until one runs out. At that point at least one side is exhausted, so I append both remainders. Collecting characters in a list and joining keeps it O(n + m).

Related

LeetCode 917: Reverse Only Letters
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 917 note: two pointers each skip non-letters and swap when both point at letters; convert the string to a list first
LeetCode 925: Long Pressed Name
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 925 note: one pointer each over name and typed; every typed character either matches the next name character or repeats the previous typed character
LeetCode 821: Shortest Distance to a Character
·2 mins
LeetCode Easy Array Two-Pointers String
LeetCode 821 note: one left-to-right pass for the distance to the nearest c on the left, then a right-to-left pass taking the minimum
LeetCode 844: Backspace String Compare
·2 mins
LeetCode Easy Two-Pointers String Stack Simulation
LeetCode 844 note: simulate backspaces with a stack; for O(1) space, walk from the end and count how many characters each # skips
LeetCode 680: Valid Palindrome II
·2 mins
LeetCode Easy Two-Pointers String Greedy
LeetCode 680 note: at the first mismatch the only options are deleting the left or the right character; check whether either remainder is a palindrome
LeetCode 696: Count Binary Substrings
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 696 note: split the string into runs of equal characters; each adjacent pair of runs contributes min(previous, current) substrings