给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。
class Solution(object):
def mergeAlternately(self, word1, word2):
list =[]
if len(word1) == len(word2):
for i in range(len(word1)):
list.append(word1[i])
list.append(word2[i])
elif len(word1) > len(word2):
for i in range(len(word1)):
if i <= len(word2) - 1:
list.append(word1[i])
list.append(word2[i])
if i > len(word2) - 1:
list.append(word1[i])
elif len(word1) < len(word2):
for i in range(len(word2)):
if i <= len(word1) - 1:
list.append(word1[i])
list.append(word2[i])
if i > len(word1) - 1:
list.append(word2[i])
a = ''
for i in range (len(list)):
a = a + list[i]
return a