# 67. Add Binary

## Question

Given two binary strings, return their sum (also a binary string).

The input strings are both **non-empty** and contains only characters `1` or `0`.

**Example 1:**

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

**Example 2:**

```
Input: a = "1010", b = "1011"
Output: "10101"
```

## Complexity

* Time complexity: O(n)
* Space complexity: O(1)

## Code

```python
def addBinary(self, a: str, b: str) -> str:
    # expand two string to same length
    a = "0" * (len(b) - len(a)) + a 
    b = "0" * (len(a) - len(b)) + b
    
    result, carry = "", 0
    for i in range(len(a) - 1, -1, -1):
        ans = int(a[i]) + int(b[i]) + carry
        carry = ans // 2 #floor division
        ans %= 2
        result = str(ans) + result
    if carry:
        result = "1" + result 
    return result
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://feifeizheng.gitbook.io/leetcode/string/add-binary.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
