파비의 매일매일 공부기록

2023.04.18 Today's Challenge 본문

Problem Solving/LeetCode

2023.04.18 Today's Challenge

fabichoi 2023. 4. 18. 23:45

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

 

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:
        res = []
        l1, l2 = len(word1), len(word2)         
        for i in range(max(l1, l2)):
            if l1 > i:
                res.append(word1[i])
            if l2 > i:
                res.append(word2[i])
        return ''.join(res)
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.04.20 Today's Challenge  (0) 2023.04.20
2023.04.19 Today's Challenge  (0) 2023.04.19
2023.04.17 Today's Challenge  (0) 2023.04.17
2023.04.16 Today's Challenge  (0) 2023.04.16
2023.04.15 Today's Challenge  (0) 2023.04.15
Comments