# **Leetcode筆記(Merge Strings Alternately)** :::info :information_source: 題目 : Merge Strings Alternately, 類型 : string , 等級 : easy 日期 : 2023/12/07,2024/05/25 ::: ### 嘗試 ```python """ i1, i2 word1 = "ab", word2 = "pqrc" while i1 < len(word1) and i2 < len(word2): while i1 != len(word1) - 1: res += word1[i1] i1 += 1 return res """ class Solution: def mergeAlternately(self, word1: str, word2: str) -> str: i1, i2 = 0, 0 res = "" while i1 < len(word1) and i2 < len(word2): res += word1[i1] res += word2[i2] i1 += 1 i2 += 1 while i1 < len(word1): res += word1[i1] i1 += 1 while i2 < len(word2): res += word2[i2] i2 += 1 return res ``` 2024/05/25 ```python class Solution: def mergeAlternately(self, word1: str, word2: str) -> str: res = "" i1, i2 = 0, 0 l1, l2 = len(word1), len(word2) while i1 < l1 and i2 < l2: res += word1[i1] res += word2[i2] i1 += 1 i2 += 1 if i1 == l1: res += word2[i2:] else: res += word1[i1:] return res ``` --- ### **優化** ```python ``` --- **思路** **講解連結** Provided by.