Skip to main content
Back to problems
Leetcode
Medium
Strings
Math
Bit Manipulation
Add Binary

Add two binary strings and return the sum as a binary string.

Acceptance 0%
Problem Statement

Add Binary

You are given two binary strings, a and b, representing non-negative integers in base 2. Return their sum, also as a binary string.

The result should not contain leading zeros unless the value is exactly 0.

Goal

Implement a function that performs binary addition without converting the entire inputs to built-in integer types when the values may be too large.

Input Format

  • Two binary strings a and b
  • Each string contains only '0' and '1'
  • The strings represent non-negative integers

Output Format

  • Return a binary string representing a + b

Constraints

  • 1 <= a.length, b.length
  • Inputs contain only '0' and '1'
  • Output must have no leading zeros except for the value 0
Examples
Sample cases returned by the problem API.

Example 1

Input

a = "11", b = "1"

Output

"100"

Explanation

11₂ + 1₂ = 100₂.

Example 2

Input

a = "1010", b = "1011"

Output

"10101"

Explanation

1010₂ + 1011₂ = 10101₂.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.