Given two strings S1 and S2 as input, the task is to merge them alternatively i.e. the first character of S1 then the first character of S2 and so on till the strings end.
NOTE: Add the whole string if other string is empty.
Example 1:
Input:
S1 = "Hello" S2 = "Bye"
Output: HBeylelo
Explanation: The characters of both the 
given strings are arranged alternatlively.
Ã¢â¬â¹Example 2:
Input: 
S1 = "abc", S2 = "def"
Output: adbecf
Explanation: The characters of both the
given strings are arranged alternatlively.
Your Task:
You don't need to read input or print anything. Your task is to complete the function merge() which takes the strings S1 and S2 as input and returns the resultant string by merging both S1 and S2 alternatively starting from S1.
Expected Time Complexity: O(|S1| + |S2|).
Expected Auxiliary Space: O(1).
Constraints:
1<=|S1|, |S2| <=10^{3}

Difficulty: EASY
Tags: String algorithms

Data structures
Skill types: Data structures
Expected Time Complexity: O(|S1| + |S2|).
Expected Auxiliary Space: O(1).

Starter code:
#User function Template for python3
class Solution:
    def merge(self, S1, S2):
        # code here

Solutions:
class Solution:

	def merge(self, S1, S2):
		merged_string = ''
		i = 0
		while i < len(S1) or i < len(S2):
			if i < len(S1):
				merged_string += S1[i]
			if i < len(S2):
				merged_string += S2[i]
			i += 1
		return merged_string


class Solution:

	def merge(self, S1, S2):
		i = 0
		ans_str = ''
		while i < len(S1) and i < len(S2):
			ans_str += S1[i]
			ans_str += S2[i]
			i += 1
		ans_str += S1[i:]
		ans_str += S2[i:]
		return ans_str


class Solution:

	def merge(self, S1, S2):
		s = ''
		min_len = min(len(S1), len(S2))
		for i in range(min_len):
			s = s + (S1[i] + S2[i])
		if len(S1) < len(S2):
			return s + S2[min_len:]
		else:
			return s + S1[min_len:]


class Solution:

	def merge(self, s1, s2):
		(i, j) = (0, 0)
		r = ''
		while i < len(s1) and j < len(s2):
			r += s1[i] + s2[j]
			j += 1
			i += 1
		r += s1[i:len(s1)]
		r += s2[j:len(s2)]
		return r


class Solution:

	def merge(self, s1, s2):
		merged = ''
		i = 0
		while i < len(s1) and i < len(s2):
			merged += s1[i] + s2[i]
			i += 1
		if i < len(s1):
			merged += s1[i:]
		if i < len(s2):
			merged += s2[i:]
		return merged


class Solution:

	def merge(self, S1, S2):
		S3 = ''
		length = min(len(S1), len(S2))
		for i in range(length):
			S3 += S1[i] + S2[i]
		if len(S1) > length:
			S3 += S1[length:]
		else:
			S3 += S2[length:]
		return S3


class Solution:

	def merge(self, S1, S2):
		i = j = 0
		res = ''
		l1 = len(S1)
		l2 = len(S2)
		while i < l1 or j < l2:
			if i < l1:
				res += S1[i]
				i += 1
			if j < l2:
				res += S2[j]
				j += 1
		return res


class Solution:

	def merge(self, S1, S2):
		s = ''
		a = len(S1)
		b = len(S2)
		i = 0
		j = 0
		while i < a and j < b:
			s += S1[i] + '' + S2[j]
			i += 1
			j += 1
		while i < a:
			s += S1[i]
			i += 1
		while j < b:
			s += S2[j]
			j += 1
		return s


class Solution:

	def merge(self, S1, S2):
		n1 = len(S1)
		n2 = len(S2)
		n = min(n1, n2)
		res = ''
		for i in range(n):
			res += S1[i] + S2[i]
		if n1 > n2:
			res += S1[n:n1]
		else:
			res += S2[n:n2]
		return res


class Solution:

	def merge(self, S1, S2):
		n1 = len(S1)
		n2 = len(S2)
		i = j = 0
		str = ''
		c = 0
		if n1 >= n2:
			while j < n2:
				if c % 2 == 0:
					str += S1[i]
					i += 1
				else:
					str += S2[j]
					j += 1
				c += 1
			str += S1[n2:]
			return str
		else:
			while i < n1:
				if c % 2 == 0:
					str += S1[i]
					i += 1
				else:
					str += S2[j]
					j += 1
				c += 1
			str += S2[n1 - 1:]
			return str


class Solution:

	def merge(self, S1, S2):
		res = ''
		i = 0
		j = 0
		k = 0
		while i < len(S1) and j < len(S2):
			if k % 2 == 0:
				res += S1[i]
				i += 1
				k += 1
			else:
				res += S2[j]
				j += 1
				k += 1
		while i < len(S1):
			res += S1[i]
			i += 1
		while j < len(S2):
			res += S2[j]
			j += 1
		return res


class Solution:

	def merge(self, S1, S2):
		res = ''
		for i in range(max(len(S1), len(S2))):
			if i < len(S1):
				res = res + S1[i]
			if i < len(S2):
				res += S2[i]
		return res


class Solution:

	def merge(self, S1, S2):
		s = ''
		x = len(S2) if len(S1) > len(S2) else len(S1)
		i = 0
		while i < x:
			s += S1[i]
			s += S2[i]
			i += 1
		while i < len(S1):
			s += S1[i]
			i += 1
		while i < len(S2):
			s += S2[i]
			i += 1
		return s


class Solution:

	def merge(self, S1, S2):
		minn = len(S1) if len(S1) < len(S2) else len(S2)
		s = ''
		for i in range(minn):
			if i < len(S1) and i < len(S2):
				s += S1[i] + S2[i]
		s += S1[minn:]
		s += S2[minn:]
		return s


class Solution:

	def merge(self, S1, S2):
		i = 0
		j = 0
		s = ''
		while len(s) < len(S1) + len(S2):
			if i < len(S1):
				s += S1[i]
				i += 1
			if j < len(S2):
				s += S2[j]
				j += 1
		return s


class Solution:

	def merge(self, S1, S2):
		result = ''
		for i in range(min(len(S1), len(S2))):
			result += S1[i] + S2[i]
		if len(S1) > len(S2):
			result += S1[i + 1:]
		elif len(S2) > len(S1):
			result += S2[i + 1:]
		return result


class Solution:

	def merge(self, S1, S2):
		result = ''
		i = 0
		while i < len(S1) and i < len(S2):
			result += S1[i] + S2[i]
			i += 1
		if i < len(S1):
			result += S1[i:]
		elif i < len(S2):
			result += S2[i:]
		return result


class Solution:

	def merge(self, S1, S2):
		output = ''
		i = 0
		j = 0
		while i < len(S1) or j < len(S2):
			if i != len(S1):
				output += S1[i]
				i += 1
			if j != len(S2):
				output += S2[j]
				j += 1
		return output


class Solution:

	def merge(self, S1, S2):
		a = len(S1)
		b = len(S2)
		if a > b:
			v = ''
			w = a - b
			r = S1[:b]
			t = S2
			d = [(i, j) for (i, j) in zip(r, t)]
			for i in d:
				for j in i:
					v += j
			return v + S1[-w:]
		elif a < b:
			v = ''
			w = b - a
			r = S2[:a]
			t = S1
			d = [(i, j) for (i, j) in zip(t, r)]
			for i in d:
				for j in i:
					v += j
			return v + S2[-w:]
		else:
			v = ''
			r = S1[:b]
			t = S2
			d = [(i, j) for (i, j) in zip(r, t)]
			for i in d:
				for j in i:
					v += j
			return v


class Solution:

	def merge(self, S1, S2):
		x = ''
		if S1 != '' and S2 != '':
			if len(S1) > len(S2):
				for i in range(len(S2)):
					x += S1[i]
					x += S2[i]
				x += S1[i + 1:]
			elif len(S1) < len(S2):
				for i in range(len(S1)):
					x += S1[i]
					x += S2[i]
				x += S2[i + 1:]
			else:
				for i in range(len(S1)):
					x += S1[i]
					x += S2[i]
		elif S1 == '':
			x = S2
		elif S2 == '':
			x = S1
		elif S1 == '' and S2 == '':
			pass
		else:
			pass
		return x


class Solution:

	def merge(self, S1, S2):
		a = ''
		x = len(S1)
		y = len(S2)
		for i in range(min(x, y)):
			a = a + S1[i] + S2[i]
		if x > y:
			a = a + S1[y:]
		else:
			a = a + S2[x:]
		return a


class Solution:

	def merge(self, S1, S2):
		s = ''
		for i in range(min(len(S1), len(S2))):
			s += S1[i] + S2[i]
		return s + S1[min(len(S1), len(S2)):] + S2[min(len(S1), len(S2)):]


class Solution:

	def merge(self, S1, S2):
		lst = []
		if len(S1) == len(S2):
			for (i, j) in zip(S1, S2):
				if i is not None and j is not None:
					lst.append(i)
					lst.append(j)
				elif i is None:
					lst.append(j)
				elif j is None:
					lst.append(i)
		elif len(S1) > len(S2):
			len_ = len(S2)
			S1_new = S1[:len_]
			S1_last = S1[len_:]
			for (i, j) in zip(S1_new, S2):
				lst.append(i)
				lst.append(j)
			lst.append(S1_last)
		else:
			len_ = len(S1)
			S2_new = S2[:len_]
			S2_last = S2[len_:]
			for (i, j) in zip(S1, S2_new):
				lst.append(i)
				lst.append(j)
			lst.append(S2_last)
		result = ''.join(map(str, lst))
		return result


class Solution:

	def merge(self, s1, s2):
		s1 = list(s1)
		s2 = list(s2)
		n = len(s1)
		m = len(s2)
		(i, j) = (0, 0)
		c = 0
		l = []
		while i != n and j != m:
			if c % 2 == 0:
				l.append(s1[i])
				i += 1
			else:
				l.append(s2[j])
				j += 1
			c += 1
		if i != n:
			l = l + s1[i:]
		else:
			l = l + s2[j:]
		s = ''.join([str(elem) for elem in l])
		return s


class Solution:

	def merge(self, S1, S2):
		a = list(S1)
		b = list(S2)
		ans = []
		p = len(a)
		m = len(b)
		n = min(p, m)
		for i in range(0, n):
			ans.append(a[i])
			ans.append(b[i])
		ans = ''.join(ans)
		if p > m:
			ans = ans + S1[n:]
			return ans
		else:
			ans = ans + S2[n:]
			return ans


class Solution:

	def merge(self, S1, S2):
		merged = ''
		(len_S1, len_S2) = (len(S1), len(S2))
		shorter_len = min(len_S1, len_S2)
		for i in range(shorter_len):
			merged += S1[i] + S2[i]
		if len_S1 > len_S2:
			merged += S1[shorter_len:]
		else:
			merged += S2[shorter_len:]
		return merged


class Solution:

	def merge(self, S1, S2):
		a = S1
		b = S2
		c = ''
		n = len(a)
		m = len(b)
		i = 0
		d = n + m
		while len(c) < d:
			if i < n:
				c += a[i]
			if i < m:
				c += b[i]
			i += 1
		return c


class Solution:

	def merge(self, s1, s2):
		s1 = list(s1)
		j = 1
		for i in s2:
			s1.insert(j, i)
			j += 2
		s2 = ''
		for i in s1:
			s2 += i
		return s2


class Solution:

	def merge(self, S1, S2):
		c = ''
		n = len(S1)
		m = len(S2)
		i = 0
		while i < n or i < m:
			if i < n:
				c += S1[i]
			if i < m:
				c += S2[i]
			i += 1
		return c


class Solution:

	def merge(self, S1, S2):
		n = len(S1)
		m = len(S2)
		ans = ''
		i = 0
		j = 0
		if n == 0:
			return S2
		if m == 0:
			return S1
		while i < n and j < m:
			ans += S1[i]
			i += 1
			ans += S2[j]
			j += 1
		while i < n:
			ans += S1[i]
			i += 1
		while j < m:
			ans += S2[j]
			j += 1
		return ans


from itertools import zip_longest

class Solution:

	def merge(self, S1, S2):
		z = list(zip_longest(S1, S2))
		s = ''
		for i in z:
			try:
				s += i[0] + i[1]
			except:
				try:
					s += i[0]
				except:
					s += i[1]
		return s


class Solution:

	def merge(self, S1, S2):
		a = ''
		i = 0
		j = 0
		while i < len(S1) and j < len(S2):
			a += S1[i] + S2[j]
			i += 1
			j += 1
		if i < len(S1):
			a += S1[i:]
		if j < len(S2):
			a += S2[j:]
		return a


class Solution:

	def merge(self, S1, S2):
		flag = 0
		i = 0
		j = 0
		S = ''
		l1 = len(S1)
		l2 = len(S2)
		while i < l1 and j < l2:
			if flag == 0:
				S += S1[i]
				i += 1
				flag = 1
			else:
				S += S2[j]
				j += 1
				flag = 0
		while i < l1:
			S += S1[i]
			i += 1
		while j < l2:
			S += S2[j]
			j += 1
		return S


class Solution:

	def merge(self, S1, S2):
		out = []
		output = []
		if len(S1) == len(S2):
			for i in range(len(S1)):
				out.append(S1[i])
				out.append(S2[i])
			out = ''.join(out)
		elif len(S1) == 0 or len(S2) == 0:
			out = S1 + S2
		elif len(S1) < len(S2):
			for l in range(len(S1)):
				out.append(S1[l])
				out.append(S2[l])
			out += S2[l + 1:]
			out = ''.join(out)
		elif len(S2) < len(S1):
			for l in range(len(S2)):
				out.append(S1[l])
				out.append(S2[l])
			out += S1[l + 1:]
			out = ''.join(out)
		return out


class Solution:

	def merge(self, s1, s2):
		i = 0
		j = 0
		s = ''
		c = 2
		while i < len(s1) and j < len(s2):
			if c % 2 == 0:
				s += s1[i]
				i += 1
				c += 1
			else:
				s += s2[j]
				j += 1
				c += 1
		while i < len(s1):
			s += s1[i]
			i += 1
		while j < len(s2):
			s += s2[j]
			j += 1
		return s


class Solution:

	def merge(self, S1, S2):
		a = ''
		min = S1
		max = S2
		if len(S2) < len(S1):
			min = S2
			max = S1
		start = 0
		while start < len(min):
			a = a + S1[start] + S2[start]
			start += 1
		a = a + max[start:len(max)]
		return a


class Solution:

	def merge(self, s1, s2):
		result = ''
		n = max(len(s1), len(s2))
		for i in range(n):
			if i < len(s1):
				result += s1[i]
			if i < len(s2):
				result += s2[i]
		return result


class Solution:

	def merge(self, S1, S2):
		if len(S1) > len(S2):
			iter = len(S1)
		else:
			iter = len(S2)
		li = []
		for i in range(iter):
			if i < len(S1):
				li.append(S1[i])
			if i < len(S2):
				li.append(S2[i])
		return ''.join(li)


class Solution:

	def merge(self, S1, S2):
		i = 0
		j = 0
		v = ''
		while i < len(S1) and j < len(S2):
			v += S1[i]
			v += S2[j]
			i += 1
			j += 1
		while i < len(S1):
			v += S1[i]
			i += 1
		while j < len(S2):
			v += S2[j]
			j += 1
		return v


class Solution:

	def merge(self, S1, S2):
		result = ''
		if S1 == '':
			return result + S2
		if S2 == '':
			return result + S1
		(i, j, count) = (0, 0, 0)
		while count < len(S1) + len(S2):
			if count % 2 == 0:
				result += S1[i]
				i += 1
				if i == len(S1):
					break
			else:
				result += S2[j]
				j += 1
				if j == len(S2):
					break
			count += 1
		if i < len(S1):
			result += S1[i:]
		if j < len(S2):
			result += S2[j:]
		return result


class Solution:

	def merge(self, word1, word2):
		ans = []
		i = 0
		j = 0
		while i < len(word1) and j < len(word2):
			ans.append(word1[i])
			ans.append(word2[j])
			i += 1
			j += 1
		while i < len(word1):
			ans.append(word1[i])
			i += 1
		while j < len(word2):
			ans.append(word2[j])
			j += 1
		return ''.join(ans)


class Solution:

	def merge(self, S1, S2):
		x = ''
		flag = True
		if len(S1) < len(S2):
			flag = False
			z = len(S1)
		else:
			z = len(S2)
		if flag == False:
			k = len(S2)
		else:
			k = len(S1)
		for i in range(0, k):
			if i < z:
				x = x + S1[i] + S2[i]
			elif z == len(S2):
				x = x + S1[i]
			else:
				x = x + S2[i]
		return x


class Solution:

	def merge(self, S1, S2):
		(i, j) = (0, 0)
		res = []
		while i < len(S1) and j < len(S2):
			res.append(S1[i])
			res.append(S2[j])
			i += 1
			j += 1
		res.append(S1[i:])
		res.append(S2[j:])
		return ''.join(res)


class Solution:

	def merge(self, S1, S2):
		ans = ''
		if len(S1) > len(S2):
			for i in range(len(S2)):
				ans += S1[i]
				ans += S2[i]
			ans += S1[i + 1:]
		else:
			for i in range(len(S1)):
				ans += S1[i]
				ans += S2[i]
			ans += S2[i + 1:]
		return ans


class Solution:

	def merge(self, S1, S2):
		s3 = ''
		s4 = ''
		if len(S1) >= len(S2):
			i = 0
			while i < len(S2):
				s3 = s3 + (S1[i] + S2[i])
				i += 1
			for i in range(len(S2), len(S1), 1):
				s4 = s4 + S1[i]
			return s3 + s4
		else:
			i = 0
			while i < len(S1):
				s3 = s3 + (S1[i] + S2[i])
				i += 1
			for i in range(len(S1), len(S2), 1):
				s4 = s4 + S2[i]
			return s3 + s4


class Solution:

	def merge(self, S1, S2):
		n1 = len(S1)
		n2 = len(S2)
		merged = ''
		i = 0
		j = 0
		while i < n1 and j < n2:
			merged += S1[i]
			merged += S2[j]
			i += 1
			j += 1
		if i < n1:
			merged += S1[i:]
		elif j < n2:
			merged += S2[j:]
		return merged


class Solution:

	def merge(self, s1, s2, st=''):
		l1 = len(s1)
		l2 = len(s2)
		if l1 < l2:
			for i in range(l1):
				st = st + s1[i] + s2[i]
			else:
				st += s2[l1:]
		else:
			for i in range(l2):
				st = st + s1[i] + s2[i]
			else:
				st += s1[l2:]
		return st


class Solution:

	def merge(self, S1, S2):
		merge = []
		min_length = min(len(S1), len(S2))
		max_length = max(len(S1), len(S2))
		for i in range(0, min_length):
			merge.append(S1[i])
			merge.append(S2[i])
		if len(S1) > len(S2):
			for i in range(min_length, max_length):
				merge.append(S1[i])
		elif len(S2) > len(S1):
			for i in range(min_length, max_length):
				merge.append(S2[i])
		merge_string = ''
		return merge_string.join(merge)


class Solution:

	def merge(self, S1, S2):
		s3 = ''
		for i in range(min(len(S1), len(S2))):
			s3 += S1[i] + S2[i]
		s3 += S1[len(S2):] + S2[len(S1):]
		if len(S1 + S2) == len(s3):
			return s3


class Solution:

	def merge(self, S1, S2):
		a = ''
		k = max(len(S1), len(S2))
		for i in range(k):
			if i < len(S1):
				a += S1[i]
			if i < len(S2):
				a += S2[i]
		return a


class Solution:

	def merge(self, S1, S2):
		merged = ''
		if len(S1) > len(S2):
			for i in range(len(S2)):
				merged += S1[i] + S2[i]
			merged = merged + S1[len(S2):]
			return merged
		else:
			merged = ''
			for i in range(len(S1)):
				merged += S1[i] + S2[i]
			merged = merged + S2[len(S1):]
		return merged


class Solution:

	def merge(self, S1, S2):
		s = ''


class Solution:

	def merge(self, S1, S2):
		a = ''
		v = 0
		i = 0
		j = 0


class Solution:

	def merge(self, S1, S2):
		s = ''
		n = len(S1)
		m = len(S2)
		for ele in zip(S1, S2):
			s += ele[0] + ele[1]
		if n > m:
			s += S1[m:]
		else:
			s += S2[n:]
		return s


class Solution:

	def merge(self, S1, S2):
		c = 0
		str3 = ''
		if len(S1) > len(S2):
			for i in range(len(S1)):
				str3 += S1[i] + S2[c]
				c += 1
				if c == len(S2):
					for j in range(i + 1, len(S1)):
						str3 += S1[j]
					return str3
		else:
			for k in range(len(S2)):
				str3 += S1[c] + S2[k]
				c += 1
				if c == len(S1):
					for l in range(k + 1, len(S2)):
						str3 += S2[l]
					return str3


class Solution:

	def merge(self, S1, S2):
		l = []
		for i in range(min(len(S1), len(S2))):
			l.append(S1[i])
			l.append(S2[i])
		if len(S1) != len(S2):
			if len(S1) > len(S2):
				l.extend(S1[len(S2):])
			elif len(S2) > len(S1):
				l.extend(S2[len(S1):])
		return ''.join(l)