In [1]:
class Solution:
def mergeAlternately(self, 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
result.append(word1[i:])
result.append(word2[j:])
return "".join(result)
In [2]:
sol = Solution()
print("Output is :- ",sol.mergeAlternately("abc","pqr"))
print("Output is :- ",sol.mergeAlternately("ab","pqrs"))
print("Output is :- ",sol.mergeAlternately("abcd","pq"))
Output is :- apbqcr Output is :- apbqrs Output is :- apbqcd