[leetcode] 1758번 : Merge Strings

James·2023년 11월 5일
0

코딩 테스트

목록 보기
31/41
post-thumbnail

문제

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

풀이

[leetcode] 1758 : Merge Strings Alternately 🥈(Easy)
⏰ 걸린 시간 : 10분

  • 알고리즘 유형 : [배열]

✔️ [문제 요약]
0. 두 문자열을 번갈아 가면서 합치는 문제

✔️ [문제 풀이]
0. 두개의 문자열의 길이가 달라서 한번씩 번갈아가며 더하는데 둘 중 짧은 것까지만 더하고
1. 나머지는 뒤에 붙여주면 된다.

코드(code)

class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        word=""
        mini = min(len(word1),len(word2))
        for i in range(0,mini):
            word += word1[i]+word2[i]
        if len(word1) > len(word2) : 
            word += word1[len(word2):len(word1)]
        elif len(word1) < len(word2):
            word += word2[len(word1):len(word2)]

회고

leetcode 나쁘지 않은데 ? 백준이 더 좋은듯..

profile
의미있는 성장의 태도, 긍정적인 사고를 지닌 Deveolper

0개의 댓글