Coding Test/Python

[LeetCode 75] 1768. Merge Strings Alternately (easy, python)

lim.dev 2023. 12. 30. 03:06

https://leetcode.com/problems/merge-strings-alternately/description/?envType=study-plan-v2&envId=leetcode-75

 

Merge Strings Alternately - LeetCode

Can you solve this real interview question? Merge Strings Alternately - You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional le

leetcode.com

 

내 코드

class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        merged = ""
        word1 = [ x for x in word1 ]
        word2 = [ x for x in word2 ]
        
        while True:
            if len(word1) == 0:
                merged += ''.join(word2)
                break
            
            elif len(word2) == 0:
                merged += ''.join(word1)
                break

            merged += word1.pop(0)
            merged += word2.pop(0)

        return merged