Problem statement

https://binarysearch.com/problems/Add-Binary-Numbers/

Solution

Equal to Leetcode 0067. Add Binary

Complexity

Time complexity is O(n + m), where n and m are lengths of numbers, space complexity is O(max(m, n)), because result will have this length.

Code

class Solution:
    def solve(self, a, b):
        i, j, summ, carry = len(a) - 1, len(b) - 1, "", 0
        while i >= 0 or j >= 0 or carry:
            d1 = int(a[i]) if i >= 0 else 0
            d2 = int(b[j]) if j >= 0 else 0
            summ += str((d1 + d2 + carry) % 2)
            carry = (d1 + d2 + carry) // 2
            i, j = i-1, j-1 
        return summ[::-1]