본문 바로가기
python-algorithm

leetcode 1768. Merge Strings Alternately

by 무적김두칠 2023. 1. 11.

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

 

Merge Strings Alternately - LeetCode

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 letters onto the end of the merged string. Ret

leetcode.com

 

1
2
3
4
5
6
7
8
9
class Solution:
    def mergeAlternately(self, word1: str, word2: str-> str:
        answer = ''
        for i in range(min(len(word1), len(word2))):
            answer += word1[i]
            answer += word2[i]
        answer += word1[min(len(word1), len(word2)):]
        answer += word2[min(len(word1), len(word2)):]
        return answer
cs

두 문자열중에서 짧은 문자열을 기준으로 merge 하시면 됩니다

You can merge based on the shorter string among the two strings.

반응형

댓글