1768. Merge Strings Alternately

Solution:

  • pretty straightforward even though I struggled a little (as this was the first problem in the LeetCode 75 list.. it has been too long)
  • the most noteworthy thing is that since I am looping through word1, I only need to account for the case where word2 is longer than word1 after the loop ends.
class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        ret = ""
        i = 0
        while i < len(word1):
            ret += word1[i]
            if i < len(word2):
                ret += word2[i]
            i += 1

        if i < len(word2):
            ret += word2[i:]
        return ret