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.
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 2180. Count Integers With Even Digit Sum (0) | 2023.01.12 |
---|---|
leetcode2351. First Letter to Appear Twice (0) | 2023.01.11 |
leetcode 728. Self Dividing Numbers (0) | 2023.01.10 |
leetcode 2427. Number of Common Factors (0) | 2023.01.10 |
leetcode 2529. Maximum Count of Positive Integer and Negative Integer (0) | 2023.01.10 |
댓글