67. Add Binary

Given two binary strings a and b, return their sum as a binary string.

Example 1:
Input: a = "11", b = "1"
Output: "100"

Example 2:
Input: a = "1010", b = "1011"
Output: "10101"

def add_binary(a, b):
    result = ""
    carry = 0
    
    # Iterate through the strings from right to left
    i, j = len(a) - 1, len(b) - 1
    
    # Perform binary addition
    while i >= 0 or j >= 0 or carry:
        # Extract digits from the strings or use 0 if the string is exhausted
        digit_a = int(a[i]) if i >= 0 else 0
        digit_b = int(b[j]) if j >= 0 else 0
        
        # Calculate the sum and carry
        total_sum = digit_a + digit_b + carry
        result = str(total_sum % 2) + result
        carry = total_sum // 2
        
        # Move to the next digits in the strings
        i -= 1
        j -= 1
    
    return result