""" 1768.1 - Merge Strings Alternately - Solution 1 - Two Pointer Approach """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def mergeAlternately(self, word1: str, word2: str) -> str:
"""Merge Strings Alternately Function"""
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)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().mergeAlternately("abc", "pqr")) # "apbqcr"
print(Solution().mergeAlternately("ab", "pqrs")) # "apbqrs"
print(Solution().mergeAlternately("abcd", "pq")) # "apbqcd"
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1768.2 - Merge Strings Alternately - Solution 2 - Using zip_longest """
#####################################################################################
# Imports
#####################################################################################
from itertools import zip_longest
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def mergeAlternately(self, word1: str, word2: str) -> str:
"""Merge Strings Alternately Function"""
return "".join(a + b for a, b in zip_longest(word1, word2, fillvalue=""))
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().mergeAlternately("abc", "pqr")) # "apbqcr"
print(Solution().mergeAlternately("ab", "pqrs")) # "apbqrs"
print(Solution().mergeAlternately("abcd", "pq")) # "apbqcd"
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()