-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeAlternately.py
More file actions
38 lines (31 loc) · 1.05 KB
/
mergeAlternately.py
File metadata and controls
38 lines (31 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
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.
Return the merged string.
Example:
Input: word1 = "abc", word2 = "pqr"
Output: "apbqcr"
Explanation: The merged string will be merged as so:
word1: a b c
word2: p q r
merged: a p b q c r
Stats:
Runtime: 53 ms, faster than 30.31% of Python3 online submissions for Merge Strings Alternately.
Memory Usage: 13.8 MB, less than 98.36% of Python3 online submissions for Merge Strings Alternately.
"""
class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
i = j = 0
res = ''
while i < len(word1) and j < len(word2):
res += word1[i]
i += 1
res += word2[j]
j += 1
while i < len(word1):
res += word1[i]
i += 1
while j < len(word2):
res += word2[j]
j += 1
return res