Skip to content

Commit bdd3051

Browse files
committed
feat: add Add Binary problem
1 parent 9846d96 commit bdd3051

File tree

7 files changed

+208
-1
lines changed

7 files changed

+208
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"problem_name": "add_binary",
3+
"solution_class_name": "Solution",
4+
"problem_number": "67",
5+
"problem_title": "Add Binary",
6+
"difficulty": "Easy",
7+
"topics": "Math, String, Bit Manipulation, Simulation",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given two binary strings `a` and `b`, return *their sum as a binary string*.",
10+
"readme_examples": [
11+
{ "content": "```\nInput: a = \"11\", b = \"1\"\nOutput: \"100\"\n```" },
12+
{ "content": "```\nInput: a = \"1010\", b = \"1011\"\nOutput: \"10101\"\n```" }
13+
],
14+
"readme_constraints": "- `1 <= a.length, b.length <= 10^4`\n- `a` and `b` consist only of `'0'` or `'1'` characters.\n- Each string does not contain leading zeros except for the zero itself.",
15+
"readme_additional": "",
16+
"solution_imports": "",
17+
"solution_methods": [
18+
{
19+
"name": "add_binary",
20+
"parameters": "a: str, b: str",
21+
"return_type": "str",
22+
"dummy_return": "\"\""
23+
}
24+
],
25+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
26+
"test_class_name": "AddBinary",
27+
"test_helper_methods": [
28+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
29+
],
30+
"test_methods": [
31+
{
32+
"name": "test_add_binary",
33+
"parametrize": "a, b, expected",
34+
"parametrize_typed": "a: str, b: str, expected: str",
35+
"test_cases": "[('11', '1', '100'), ('1010', '1011', '10101'), ('0', '0', '0'), ('1', '1', '10'), ('1111', '1111', '11110')]",
36+
"body": "result = self.solution.add_binary(a, b)\nassert result == expected"
37+
}
38+
],
39+
"playground_imports": "from solution import Solution",
40+
"playground_test_case": "# Example test case\na = '11'\nb = '1'\nexpected = '100'",
41+
"playground_execution": "result = Solution().add_binary(a, b)\nresult",
42+
"playground_assertion": "assert result == expected"
43+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= maximum_depth_of_binary_tree
2+
PROBLEM ?= add_binary
33
FORCE ?= 0
44
COMMA := ,
55

leetcode/add_binary/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Add Binary
2+
3+
**Difficulty:** Easy
4+
**Topics:** Math, String, Bit Manipulation, Simulation
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 67](https://leetcode.com/problems/add-binary/description/)
8+
9+
## Problem Description
10+
11+
Given two binary strings `a` and `b`, return _their sum as a binary string_.
12+
13+
## Examples
14+
15+
### Example 1:
16+
17+
```
18+
Input: a = "11", b = "1"
19+
Output: "100"
20+
```
21+
22+
### Example 2:
23+
24+
```
25+
Input: a = "1010", b = "1011"
26+
Output: "10101"
27+
```
28+
29+
## Constraints
30+
31+
- `1 <= a.length, b.length <= 10^4`
32+
- `a` and `b` consist only of `'0'` or `'1'` characters.
33+
- Each string does not contain leading zeros except for the zero itself.

leetcode/add_binary/__init__.py

Whitespace-only changes.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "setup",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Example test case\n",
21+
"a = \"11\"\n",
22+
"b = \"1\"\n",
23+
"expected = \"100\""
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 3,
29+
"id": "execute",
30+
"metadata": {},
31+
"outputs": [
32+
{
33+
"data": {
34+
"text/plain": [
35+
"'100'"
36+
]
37+
},
38+
"execution_count": 3,
39+
"metadata": {},
40+
"output_type": "execute_result"
41+
}
42+
],
43+
"source": [
44+
"result = Solution().add_binary(a, b)\n",
45+
"result"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 4,
51+
"id": "test",
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"assert result == expected"
56+
]
57+
}
58+
],
59+
"metadata": {
60+
"kernelspec": {
61+
"display_name": "leetcode-py-py3.13",
62+
"language": "python",
63+
"name": "python3"
64+
},
65+
"language_info": {
66+
"codemirror_mode": {
67+
"name": "ipython",
68+
"version": 3
69+
},
70+
"file_extension": ".py",
71+
"mimetype": "text/x-python",
72+
"name": "python",
73+
"nbconvert_exporter": "python",
74+
"pygments_lexer": "ipython3",
75+
"version": "3.13.7"
76+
}
77+
},
78+
"nbformat": 4,
79+
"nbformat_minor": 5
80+
}

leetcode/add_binary/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
# Time: O(len(a) + len(b))
3+
# Space: O(len(a) + len(b))
4+
def add_binary(self, a: str, b: str) -> str:
5+
# int(a, 2) converts binary string to decimal: int("11", 2) → 3
6+
# bin() converts decimal to binary string with prefix: bin(3) → '0b11'
7+
# [2:] removes the '0b' prefix to get just binary digits
8+
return bin(int(a, 2) + int(b, 2))[2:]
9+
10+
11+
# Python Base Conversion:
12+
#
13+
# String → Integer (using int() with base parameter):
14+
# - int("1010", 2) → 10 (binary to decimal)
15+
# - int("ff", 16) → 255 (hex to decimal)
16+
# - int("10", 8) → 8 (octal to decimal)
17+
#
18+
# Integer → String (conversion functions add prefixes):
19+
# - bin(10) → '0b1010' (binary with '0b' prefix)
20+
# - hex(255) → '0xff' (hex with '0x' prefix)
21+
# - oct(8) → '0o10' (octal with '0o' prefix)
22+
#
23+
# These prefixes match Python literal syntax:
24+
# - 0b1010 = 10, 0xff = 255, 0o10 = 8
25+
#
26+
# For string problems, slice off the prefix: bin(n)[2:] gives just the digits.

leetcode/add_binary/tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
3+
from leetcode_py.test_utils import logged_test
4+
5+
from .solution import Solution
6+
7+
8+
class TestAddBinary:
9+
def setup_method(self):
10+
self.solution = Solution()
11+
12+
@pytest.mark.parametrize(
13+
"a, b, expected",
14+
[
15+
("11", "1", "100"),
16+
("1010", "1011", "10101"),
17+
("0", "0", "0"),
18+
("1", "1", "10"),
19+
("1111", "1111", "11110"),
20+
],
21+
)
22+
@logged_test
23+
def test_add_binary(self, a: str, b: str, expected: str):
24+
result = self.solution.add_binary(a, b)
25+
assert result == expected

0 commit comments

Comments
 (0)