Given two binary strings A and B consisting of only 0s and 1s. Find the resultant string after adding the two Binary Strings.
Note: The input strings may contain leading zeros but the output string should not have any leading zeros.
Example 1:
Input:
A = "1101", B = "111"
Output: 10100
Explanation:
 1101
+ 111
10100
Example 2:
Input: 
A = "10", B = "01"
Output: 11
Explanation: 
  10
+ 01
  11
Your Task:
You don't need to read input or print anything. Your task is to complete the function addBinary() which takes 2 binary string A and B and returns a binary string denoting the addition of both the strings.
Expected Time Complexity: O(max(|A|, |B|)).
Expected Auxiliary Space: O(max(|A|, |B|)) (for output string).
Constraints:
1 ≤ |A|, |B| ≤ 10^{6}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:

	def addBinary(self, A, B):

		# code here

Solutions:
class Solution:

	def addBinary(self, A, B):
		sum = bin(int(a, 2) + int(b, 2))
		return sum[2:]


class Solution:

	def addBinary(self, A, B):
		sums = bin(int(A, 2) + int(B, 2))
		return str(sums[2:])


class Solution:

	def addBinary(self, a, b):
		s = bin(int(a, 2) + int(b, 2))
		return s[2:]


class Solution:

	def addBinary(self, A, B):
		new_A = int(A, 2)
		new_B = int(B, 2)
		s = new_A + new_B
		return bin(s).replace('0b', '')


class Solution:

	def addBinary(self, A, B):
		A = int(A, 2)
		B = int(B, 2)
		C = A + B
		return bin(C)[2:]


class Solution:

	def addBinary(self, A, B):
		a = int(A, 2)
		b = int(B, 2)
		c = a + b
		g = bin(c)
		g = g[2:]
		return g


class Solution:

	def addBinary(self, A, B):
		if len(A) > len(B):
			B = B.zfill(len(A))
		if len(A) < len(B):
			A = A.zfill(len(B))
		c = '0'
		res = ''
		for i in range(len(A) - 1, -1, -1):
			if A[i] == '1' and B[i] == '1' and (c == '1'):
				res += '1'
				c = '1'
			elif A[i] == '1' and B[i] == '1' and (c == '0'):
				res += '0'
				c = '1'
			elif A[i] == '0' and B[i] == '0' and (c == '1'):
				res += '1'
				c = '0'
			elif A[i] == '0' and B[i] == '0' and (c == '0'):
				res += '0'
				c = '0'
			elif c == '1':
				res += '0'
				c = '1'
			else:
				res += '1'
				c = '0'
		if c == '1':
			res += '1'
		res = res[::-1]
		res = res.lstrip('0')
		return res


class Solution:

	def sum_bin(self, a, b):
		if a == '1' and b == '1':
			return ('1', '0')
		if a == '1' or b == '1':
			return ('0', '1')
		return ('0', '0')

	def addBinary(self, A, B):
		i = len(A) - 1
		j = len(B) - 1
		res = ''
		carry = '0'
		while i >= 0 and j >= 0:
			(carry1, r1) = self.sum_bin(A[i], B[j])
			(carry, r) = self.sum_bin(r1, carry)
			if carry == '1' or carry1 == '1':
				carry = '1'
			res = res + r
			i = i - 1
			j = j - 1
		while i >= 0:
			(carry, r) = self.sum_bin(A[i], carry)
			res = res + r
			i = i - 1
		while j >= 0:
			(carry, r) = self.sum_bin(B[j], carry)
			res = res + r
			j = j - 1
		if carry != '0':
			res = res + carry
		res = res[::-1]
		return res.lstrip('0')


class Solution:

	def addBinary(self, A, B):
		a = int(A, 2)
		b = int(B, 2)
		c = a + b
		z = bin(c).replace('0b', '')
		return z


class Solution:

	def addBinary(self, A, B):
		x = int(A, 2)
		y = int(B, 2)
		return format(x + y, 'b')


class Solution:

	def addBinary(self, a, b):
		n = max(len(a), len(b))
		if len(a) < len(b):
			(a, b) = (b, a)
		ans = ''
		a = a[::-1]
		b = b[::-1]
		a = list(map(int, a))
		b = list(map(int, b))
		for i in range(len(b), len(a)):
			b.append(0)
		carry = 0
		for i in range(n):
			total = a[i] + b[i] + carry
			if total <= 1:
				ans += str(total)
				carry = 0
			elif total % 2 == 0:
				ans += '0'
				carry = 1
			elif total % 2 != 0:
				ans += '1'
				carry = 1
		if carry == 1:
			ans += '1'
		ans = ans[::-1]
		for i in range(len(ans)):
			if ans[i] != '0':
				break
		return ans[i:]


class Solution:

	def addBinary(self, a, b):

		def myAdd(n1, n2):
			base = n1 ^ n2
			carry = 0
			if n1 == 1 and n2 == 1:
				carry = 1
			return (base, carry)
		carry = 0
		diff = abs(len(a) - len(b))
		if len(a) < len(b):
			a = '0' * diff + a
		elif len(b) < len(a):
			b = '0' * diff + b
		i = len(a) - 1
		ans = []
		while i > -1:
			sum1 = myAdd(int(a[i]), int(b[i]))
			sum2 = myAdd(sum1[0], carry)
			ans.append(str(sum2[0]))
			if sum1[1] > sum2[1]:
				carry = sum1[1]
			else:
				carry = sum2[1]
			i -= 1
		if carry > 0:
			ans.append(str(carry))
		ans.reverse()
		for i in range(len(ans)):
			if ans[i] == '1':
				break
		return ''.join(ans[i:])


class Solution:

	def addBinary(self, s1, s2):

		def add_b(n1, n2):
			if n1 == '0' and n2 == '0':
				b = '0'
				c = '0'
			elif n1 == '1' and n2 == '0':
				b = '1'
				c = '0'
			elif n1 == '0' and n2 == '1':
				b = '1'
				c = '0'
			else:
				b = '0'
				c = '1'
			return (b, c)
		res = []
		if len(s1) < len(s2):
			s1 = abs(len(s2) - len(s1)) * '0' + s1
		else:
			s2 = abs(len(s2) - len(s1)) * '0' + s2
		s1 = list(s1)
		s2 = list(s2)
		carry = '0'
		for i in range(len(s1) - 1, -1, -1):
			(b1, c1) = add_b(s1[i], s2[i])
			(b2, c2) = add_b(b1, carry)
			if c1 == '1':
				carry = c1
			elif c2 == '1':
				carry = c2
			else:
				carry = '0'
			base = b2
			res.append(base)
		else:
			res.append(carry)
		res.reverse()
		m = ''
		for i in range(0, len(res)):
			if res[i] == '1':
				break
		m = res[i:]
		if m == '':
			return '0'
		else:
			return ''.join(m)


class Solution:

	def addBinary(self, A, B):
		return '{0:b}'.format(int(A, 2) + int(B, 2))


class Solution:

	def addBinary(self, a, b):
		a = int(a, 2)
		b = int(b, 2)
		n = a + b
		n = bin(n)
		res = n.lstrip('0b')
		return res


from operator import *

class Solution:

	def addBinary(self, A, B):
		return bin(add(int(A, 2), int(B, 2)))[2:]


class Solution:

	def addBinary(self, A, B):
		x = int(A, 2) + int(B, 2)
		s = bin(x)
		return s[2:]


class Solution:

	def addBinary(self, A, B):
		a = int(A, base=2)
		b = int(B, base=2)
		r = a + b
		re = bin(r)
		res = re[2:]
		return res


class Solution:

	def addBinary(self, A, B):
		res = bin(int(A, 2) + int(B, 2))[2:]
		return res


class Solution:

	def addBinary(self, A, B):
		i = len(A)
		j = len(B)
		if i == 0:
			return B
		if j == 0:
			return A
		i -= 1
		j -= 1
		res = ''
		carry = '0'
		while i >= 0 or j >= 0:
			if i >= 0:
				ch1 = A[i]
			else:
				ch1 = '0'
			if j >= 0:
				ch2 = B[j]
			else:
				ch2 = '0'
			temp = int(ch1) + int(ch2) + int(carry)
			if temp == 0:
				res += '0'
				carry = '0'
			elif temp == 1:
				res += '1'
				carry = '0'
			elif temp == 2:
				res += '0'
				carry = '1'
			else:
				res += '1'
				carry = '1'
			i -= 1
			j -= 1
		if carry == '1':
			res += carry
		l = len(res)
		while l > 0 and res[l - 1] == '0':
			l -= 1
		if l == 0:
			return '0'
		res = list(res)
		for i in range(l // 2):
			(res[i], res[l - i - 1]) = (res[l - i - 1], res[i])
		res = ''.join(res)
		return res[0:l]


class Solution:

	def addBinary(self, A, B):
		a = int(A, 2)
		b = int(B, 2)
		res = a + b
		res = bin(res)
		temp = ''
		for i in range(2, len(res)):
			temp += res[i]
		return temp


class Solution:

	def addBinary(self, A, B):
		return str(bin(int(A, 2) + int(B, 2))).replace('0b', '')


class Solution:

	def addBinary(self, A, B):
		a = int(A, 2)
		b = int(B, 2)
		s = a + b
		y = str(bin(s))
		ans = y[2:]
		return ans


class Solution:

	def addBinary(self, A, B):
		sum = bin(int(A, 2) + int(B, 2))
		return sum.lstrip('0,b')


class Solution:

	def addBinary(self, A, B):
		i = len(A)
		j = len(B)
		if i == 0:
			return B
		if j == 0:
			return A
		i -= 1
		j -= 1
		res = ''
		carry = '0'
		while i >= 0 or j >= 0:
			if i >= 0:
				ch1 = A[i]
			else:
				ch1 = '0'
			if j >= 0:
				ch2 = B[j]
			else:
				ch2 = '0'
			temp = int(ch1) + int(ch2) + int(carry)
			if temp == 0:
				res += '0'
				carry = '0'
			elif temp == 1:
				res += '1'
				carry = '0'
			elif temp == 2:
				res += '0'
				carry = '1'
			else:
				res += '1'
				carry = '1'
			i -= 1
			j -= 1
		if carry == '1':
			res += carry
		l = len(res)
		while l > 0 and res[l - 1] == '0':
			l -= 1
		res = res[l - 1::-1]
		return res


class Solution:

	def addBinary(self, A, B):
		ans = bin(int(A, 2) + int(B, 2))
		return ans[2:]


class Solution:

	def addBinary(self, A, B):
		while A and A[0] == '0':
			A = A[1:]
		while B and B[0] == '0':
			B = B[1:]
		res = ''
		(c, i, j) = (0, len(A) - 1, len(B) - 1)
		while i >= 0 or j >= 0 or c > 0:
			x = int(A[i]) if i >= 0 else 0
			y = int(B[j]) if j >= 0 else 0
			v = x + y + c
			c = v // 2
			v %= 2
			res += str(v)
			(i, j) = (i - 1, j - 1)
		return res[::-1]


class Solution:

	def addBinary(self, A, B):
		k = int(A, 2)
		l = int(B, 2)
		m = bin(k + l)
		return m[2:]


class Solution:

	def addBinary(self, A, B):
		a = int(A, 2)
		b = int(B, 2)
		r = a + b
		l = bin(r)
		l = l[2:]
		return str(l)


class Solution:

	def addBinary(self, A, B):
		x = int(A, 2)
		y = int(B, 2)
		z = bin(x + y)
		return z[2:]


class Solution:

	def addBinary(self, A, B):
		if not A or not B:
			return
		carry = 0
		(i, j) = (len(A) - 1, len(B) - 1)
		res = []
		while i >= 0 or j >= 0:
			a = int(A[i]) if i >= 0 else 0
			b = int(B[j]) if j >= 0 else 0
			s = a + b + carry
			(carry, s) = divmod(s, 2)
			res.append(str(s))
			i -= 1
			j -= 1
		if carry == 1:
			res.append(str(carry))
		return ''.join(res[::-1]).lstrip('0') or '0'


from operator import *

class Solution:

	def addBinary(self, A, B):
		x = int(A, 2)
		y = int(B, 2)
		d = add(x, y)
		return bin(d)[2:]


class Solution:

	def addBinary(self, A, B):
		c = int(A, 2)
		d = int(B, 2)
		e = c + d
		e = bin(e)
		return e[2:]


class Solution:

	def addBinary(self, A, B):
		op = int(A, 2) + int(B, 2)
		return bin(op)[2:]


class Solution:

	def addBinary(self, A, B):
		a = int(A, 2)
		b = int(B, 2)
		c = a + b
		return format(c, 'b')


class Solution:

	def addBinary(self, A, B):
		(m, n) = (len(A) - 1, len(B) - 1)
		ans = ''
		carry = 0
		while m >= 0 and n >= 0:
			summ = int(A[m]) + int(B[n]) + carry
			carry = summ // 2
			ans += str(summ % 2)
			m -= 1
			n -= 1
		while m >= 0:
			summ = int(A[m]) + carry
			carry = summ // 2
			ans += str(summ % 2)
			m -= 1
		while n >= 0:
			summ = int(B[n]) + carry
			carry = summ // 2
			ans += str(summ % 2)
			n -= 1
		while carry:
			ans += str(carry % 2)
			carry = carry // 2
		ans = ans[::-1]
		i = 0
		n = len(ans)
		while i < n and ans[i] == '0':
			i += 1
		return ans[i:]


class Solution:

	def addBinary(self, A, B):
		a = int(A, 2)
		b = int(B, 2)
		result = a + b
		binary_result = bin(result)[2:]
		return binary_result


class Solution:

	def addBinary(self, A, B):
		sum1 = int(A, 2)
		sum2 = int(B, 2)
		res = sum1 + sum2
		res = bin(res)
		res = res.replace('0b', '')
		if res[0] == '0':
			return res[1:]
		else:
			return res


class Solution:

	def addBinary(self, A, B):
		num1 = int(A, 2)
		num2 = int(B, 2)
		ans = num1 + num2
		ans = bin(ans).replace('0b', '')
		return ans


class Solution:

	def addBinary(self, A, B):
		ele1 = int(A, 2)
		ele2 = int(B, 2)
		su = ele1 + ele2
		res = bin(su).replace('0b', '')
		return res


class Solution:

	def addBinary(self, A, B):
		l = max(len(A), len(B))
		(a, b) = (int(A, 2), int(B, 2))
		return str(bin(a + b))[2:]