Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
A, B = len(word1), len(word2)
a, b = 0, 0
s = []

word = 1
while a < A and b < B:
if word == 1:
s.append(word1[a])
a += 1
word = 2
else:
s.append(word2[b])
b += 1
word = 1

while a < A:
s.append(word1[a])
a += 1

while b < B:
s.append(word2[b])
b += 1

return ''.join(s)
# Time: O(A + B) - A is Length of word1, B is Length of word2
def mergeAlternately(self, word1: str, word2: str) -> str:
l = 0
res = ""
while l < len(word1) or l < len(word2):
if l < len(word1):
res += word1[l]
if l < len(word2):
res += word2[l]
l+=1
return res
# Time: O(n)
# Space: O(A + B) - A is Length of word1, B is Length of word2