Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 82805f5

Browse files
committed
Add 1768_merge_strings_alternately.py
1 parent d7574ad commit 82805f5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def mergeAlternately(self, word1: str, word2: str) -> str:
3+
result = []
4+
5+
for index in range(max(len(word1), len(word2))):
6+
if index < len(word1):
7+
result.append(word1[index])
8+
if index < len(word2):
9+
result.append(word2[index])
10+
11+
return ''.join(result)
12+
13+
assert Solution().mergeAlternately(word1="abc", word2="pqr") == "apbqcr", "Test 1 Failed"
14+
assert Solution().mergeAlternately(word1="ab", word2="pqrs") == "apbqrs", "Test 2 Failed"
15+
assert Solution().mergeAlternately(word1="abcd", word2="pq") == "apbqcd", "Test 3 Failed"

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /