1894. Merge Strings Alternately
My accepted Python solution to LeetCode problem 1894, Merge Strings Alternately, running in 37ms.
- Difficulty: Easy
- Python
- Runtime 37ms
- Memory 17.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 37ms, memory 17.2MB, accepted 2025-12-31.
class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
result = []
i = 0
while i < len(word1) or i < len(word2):
if i < len(word1):
result.append(word1[i])
if i < len(word2):
result.append(word2[i])
i += 1
return ''.join(result)