一、题目链接
二、解题思路
双指针扫描word1、word2,先添加word1的单个字符,再添加word2的单个字符,最后将剩余的字符直接添加在后面。
三、代码
class Solution(object):
def mergeAlternately(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: str
"""
result = ''
i, j = 0, 0
while (i < len(word1)) or (j<len(word2)):
if i < len(word1):
result += word1[i]
i += 1
if j < len(word2):
result += word2[j]
j += 1
return result