Skip to content

Commit b297343

Browse files
authored
Create Merge Strings Alternately - Leetcode 1768.py
1 parent 7f79dab commit b297343

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def mergeAlternately(self, word1: str, word2: str) -> str:
3+
A, B = len(word1), len(word2)
4+
a, b = 0, 0
5+
s = []
6+
7+
word = 1
8+
while a < A and b < B:
9+
if word == 1:
10+
s.append(word1[a])
11+
a += 1
12+
word = 2
13+
else:
14+
s.append(word2[b])
15+
b += 1
16+
word = 1
17+
18+
while a < A:
19+
s.append(word1[a])
20+
a += 1
21+
22+
while b < B:
23+
s.append(word2[b])
24+
b += 1
25+
26+
return ''.join(s)
27+
# Time: O(A + B) - A is Length of word1, B is Length of word2
28+
# Space: O(A + B) - A is Length of word1, B is Length of word2

0 commit comments

Comments
 (0)