VISUALGO//PROBLEMS
merge strings alternately · array / stringeasy
LeetCode #1768
word1
a
b
c
word2
p
q
r
result
·
·
·
·
·
·
ELEMENTS
word1
word2
Python
def mergeAlternately(word1: str, word2: str) -> str:
    result = []
    i, j = 0, 0

    while i < len(word1) and j < len(word2):
        result.append(word1[i])
        result.append(word2[j])
        i += 1
        j += 1

    # Append remaining characters
    result.append(word1[i:])
    result.append(word2[j:])

    return ''.join(result)