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:
merged = ""
word1 = [ x for x in word1 ]
word2 = [ x for x in word2 ]
while True:
if len(word1) == 0:
merged += ''.join(word2)
break
elif len(word2) == 0:
merged += ''.join(word1)
break
merged += word1.pop(0)
merged += word2.pop(0)
return merged
'Coding Test > Python' 카테고리의 다른 글
[백준] 14890번: 경사로 (골드3, 파이썬) (0) | 2023.12.30 |
---|---|
[LeetCode 75] 1071. Greatest Common Divisor of Strings (easy, python) (1) | 2023.12.30 |
[백준] 14888번: 연산자 끼워넣기(실버 1, 파이썬) (0) | 2023.12.29 |
[백준] 14503번: 로봇 청소기 (골드 5, 파이썬) (1) | 2023.12.29 |
[백준] 3190번: 뱀 (골드 4, 파이썬) (1) | 2023.12.22 |