Input: a = "11", b = "1"
Output: "100"
Input: a = "1010", b = "1011"
Output: "10101"
class Solution:
def addBinary(self, a: str, b: str) -> str:
res = ''
idx = carry = 0
while idx < max(len(a), len(b)) or carry:
aa = a[len(a)-1-idx] if idx < len(a) else '0'
bb = b[len(b)-1-idx] if idx < len(b) else '0'
carry, mod = divmod((int(aa) + int(bb) + carry), 2)
res = str(mod) + res
idx += 1
return res
class Solution:
def addBinary(self, a, b):
# return bin(eval('0b' + a) + eval('0b' + b))[2:]
return bin(int(a, 2) + int(b, 2))[2:]