> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Add Binary Python Solution with Tests

> Tested Python solution for LeetCode 67 with 11 pytest cases. Generate a practice environment with lcpy.

LeetCode 67, Easy. Topics: Math, String, Bit Manipulation, Simulation. [View on LeetCode](https://leetcode.com/problems/add-binary/description/).

Generate this problem as a practice environment: tested reference solution, 11 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 67   # by problem number
lcpy gen -s add_binary   # by problem name
```

## Problem

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

### Examples

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

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

### Constraints

* `1 <= a.length, b.length <= 10^4`
* `a` and `b` consist only of `'0'` or `'1'` characters.
* Each string does not contain leading zeros except for the zero itself.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/add_binary/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/add_binary/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(len(a) + len(b))
    # Space: O(len(a) + len(b))
    def add_binary(self, a: str, b: str) -> str:
        # int(a, 2) converts binary string to decimal: int("11", 2) → 3
        # bin() converts decimal to binary string with prefix: bin(3) → '0b11'
        # [2:] removes the '0b' prefix to get just binary digits
        return bin(int(a, 2) + int(b, 2))[2:]


# Python Base Conversion:
#
# String → Integer (using int() with base parameter):
# - int("1010", 2) → 10 (binary to decimal)
# - int("ff", 16) → 255 (hex to decimal)
# - int("10", 8) → 8 (octal to decimal)
#
# Integer → String (conversion functions add prefixes):
# - bin(10) → '0b1010' (binary with '0b' prefix)
# - hex(255) → '0xff' (hex with '0x' prefix)
# - oct(8) → '0o10' (octal with '0o' prefix)
#
# These prefixes match Python literal syntax:
# - 0b1010 = 10, 0xff = 255, 0o10 = 8
#
# For string problems, slice off the prefix: bin(n)[2:] gives just the digits.
```

## Complexity

| Time               | Space              |
| ------------------ | ------------------ |
| O(len(a) + len(b)) | O(len(a) + len(b)) |

## Tags

[Grind 75](/catalog/grind-75), [Grind](/catalog/grind), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
