알고리즘/Leetcode

[leetcode/python3] 1768.Merge Strings Alternately

llollhh_ 2021. 4. 10. 21:49

문제링크

leetcode.com/problems/merge-strings-alternately/

 

Merge Strings Alternately - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        res = ''
        
        arr1 = list(word1)
        arr2 = list(word2)
        
        maxLenght = max(len(word1), len(word2)) * 2
        
        for i in range(maxLenght):
            if i % 2 == 0 and arr1:
                res += arr1.pop(0)
            elif arr2:
                res += arr2.pop(0)
        
        return res