Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions problems/0067-add-binary/analysis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 0067. Add Binary

[LeetCode Link](https://leetcode.com/problems/add-binary/)

Difficulty: Easy
Topics: Math, String, Bit Manipulation, Simulation
Acceptance Rate: 56.9%

## Hints

### Hint 1

Think about how you add two numbers by hand on paper. You start from the rightmost digit and work your way left, keeping track of a carry. The same principle applies to binary addition.

### Hint 2

Use two pointers starting at the end of each string and move them leftward simultaneously. At each step, sum the two digits plus any carry from the previous step. The current digit of the result is `sum % 2`, and the new carry is `sum / 2`.

### Hint 3

The strings can have different lengths, so your loop should continue as long as there are digits remaining in either string OR there is a carry left to process. Build the result in reverse order and reverse it at the end (or prepend to the result).

## Approach

Simulate binary addition just like manual addition on paper:

1. Initialize two pointers `i` and `j` at the last index of strings `a` and `b` respectively.
2. Initialize a `carry` variable to 0.
3. Loop while `i >= 0` or `j >= 0` or `carry > 0`:
- Get the digit from `a[i]` (or 0 if `i` is out of bounds).
- Get the digit from `b[j]` (or 0 if `j` is out of bounds).
- Compute `sum = digitA + digitB + carry`.
- Append `sum % 2` to the result.
- Update `carry = sum / 2`.
- Decrement `i` and `j`.
4. Since we built the result from least significant to most significant bit, reverse the result string before returning.

**Example walkthrough** with `a = "1010"` and `b = "1011"`:

| Step | digitA | digitB | carry | sum | result digit | new carry |
|------|--------|--------|-------|-----|-------------|-----------|
| 1 | 0 | 1 | 0 | 1 | 1 | 0 |
| 2 | 1 | 1 | 0 | 2 | 0 | 1 |
| 3 | 0 | 0 | 1 | 1 | 1 | 0 |
| 4 | 1 | 1 | 0 | 2 | 0 | 1 |
| 5 | - | - | 1 | 1 | 1 | 0 |

Result (reversed): `10101`

## Complexity Analysis

Time Complexity: O(max(m, n)), where m and n are the lengths of strings `a` and `b`. We process each digit exactly once.

Space Complexity: O(max(m, n)) for the result string. The result can be at most one digit longer than the longer input.

## Edge Cases

- **Different lengths**: The strings `a` and `b` can have very different lengths (e.g., `"1"` and `"1111111111"`). The loop must handle indices going out of bounds independently for each string.
- **Final carry**: Adding `"1"` and `"1"` produces `"10"`, which is longer than both inputs. The loop must continue when carry is non-zero even after both strings are exhausted.
- **Single character inputs**: Both strings could be `"0"` or `"1"`. The algorithm should handle these without special cases.
- **All ones**: Strings like `"1111"` + `"1111"` produce a carry cascade resulting in `"11110"`. Ensures carry propagation works correctly through the entire length.
46 changes: 46 additions & 0 deletions problems/0067-add-binary/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
number: "0067"
frontend_id: "67"
title: "Add Binary"
slug: "add-binary"
difficulty: "Easy"
topics:
- "Math"
- "String"
- "Bit Manipulation"
- "Simulation"
acceptance_rate: 5693.0
is_premium: false
created_at: "2026-02-15T03:26:04.473955+00:00"
fetched_at: "2026-02-15T03:26:04.473955+00:00"
link: "https://leetcode.com/problems/add-binary/"
date: "2026-02-15"
---

# 0067. 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"




**Constraints:**

* `1 <= a.length, b.length <= 104`
* `a` and `b` consist only of `'0'` or `'1'` characters.
* Each string does not contain leading zeros except for the zero itself.
30 changes: 30 additions & 0 deletions problems/0067-add-binary/solution_daily_20260215.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

// addBinary adds two binary strings by simulating manual binary addition
// from right to left, tracking carry at each step.
func addBinary(a string, b string) string {
i, j := len(a)-1, len(b)-1
carry := 0
result := []byte{}

for i >= 0 || j >= 0 || carry > 0 {
sum := carry
if i >= 0 {
sum += int(a[i] - '0')
i--
}
if j >= 0 {
sum += int(b[j] - '0')
j--
}
result = append(result, byte(sum%2)+'0')
carry = sum / 2
}

// Reverse the result
for l, r := 0, len(result)-1; l < r; l, r = l+1, r-1 {
result[l], result[r] = result[r], result[l]
}

return string(result)
}
29 changes: 29 additions & 0 deletions problems/0067-add-binary/solution_daily_20260215_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import "testing"

func TestAddBinary(t *testing.T) {
tests := []struct {
name string
a string
b string
expected string
}{
{"example 1: simple carry", "11", "1", "100"},
{"example 2: same length addition", "1010", "1011", "10101"},
{"edge case: both zeros", "0", "0", "0"},
{"edge case: one is zero", "0", "1", "1"},
{"edge case: single ones produce carry", "1", "1", "10"},
{"different lengths", "1", "11111", "100000"},
{"all ones cascade carry", "1111", "1111", "11110"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := addBinary(tt.a, tt.b)
if result != tt.expected {
t.Errorf("addBinary(%q, %q) = %q, want %q", tt.a, tt.b, result, tt.expected)
}
})
}
}