const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Add Binary LeetCode Solution: Binary String Addition Explained

Add Binary (#67) is the string version of Add Two Numbers — same carry logic but on binary characters instead of linked list nodes. Master the carry pattern once and three problems become trivial.

6 min read|

Binary addition is just carry propagation on strings

Same pattern as Add Two Numbers — learn one, solve three

Add Binary Is Carry Propagation on Strings

If you have already solved Add Two Numbers (#2), you already know how to solve Add Binary (#67). The core mechanic is identical: process digits from right to left, add them together with a carry, and build the result one digit at a time. The only difference is that you are working with binary characters in a string instead of decimal values in linked list nodes.

Add binary leetcode problems test whether you can implement clean carry logic without reaching for language-level integer conversion. It is one of the most common easy problems on the platform and a gateway to understanding how computers handle arithmetic at the bit level.

In this walkthrough you will see the approach, a clear implementation, a visual trace through two examples, and the edge cases that trip people up in interviews.

Understanding the Add Binary Problem

Given two binary strings a and b, return their sum as a binary string. For example, given a = "11" and b = "1", the answer is "100". The inputs contain only the characters 0 and 1, and each string is non-empty with no leading zeros except for the string "0" itself.

The problem is deceptively simple. You might be tempted to convert each string to an integer, add them, and convert back. That works for short strings, but binary strings in interview problems can be thousands of characters long — far beyond what a 64-bit integer can hold.

The Approach: Right-to-Left with Carry

The algorithm mirrors how you add numbers by hand. Start at the rightmost digit of both strings. Add the two digits plus any carry from the previous column. The result digit is sum % 2 (since we are in base 2), and the new carry is sum / 2 (integer division).

Use two pointers, one starting at the end of each string. In each iteration, grab the digit at each pointer if it is still in bounds, otherwise treat it as 0. Add the two digits and the carry. Append sum % 2 to your result. Update carry to Math.floor(sum / 2). Move both pointers left.

Continue the loop while either pointer is still valid or a carry remains. When the loop ends, reverse the result string since you built it from least significant to most significant digit.

💡

Pro Tip

Build the result string in reverse (append digits right to left), then reverse at the end — this avoids expensive string prepend operations and keeps the code clean.

Implementation of the Add Binary Solution

The implementation uses two index pointers i and j starting at the last character of each string. A carry variable starts at 0. Inside a while loop that runs while i >= 0, j >= 0, or carry > 0, you compute the sum of the current digits and carry, push sum % 2 onto a result array, and update carry.

After the loop, reverse the result array and join it into a string. That is the answer. The time complexity is O(max(m, n)) where m and n are the lengths of the two input strings. The space complexity is O(1) extra beyond the output string itself.

  • Initialize i = a.length - 1, j = b.length - 1, carry = 0
  • While i >= 0 OR j >= 0 OR carry > 0: sum = carry + (a[i] or 0) + (b[j] or 0)
  • Result digit = sum % 2, new carry = Math.floor(sum / 2)
  • Decrement i and j each iteration
  • Reverse the collected digits and return as a string

Visual Walkthrough of Add Binary Examples

Walk through "11" + "1". Start from the right. Column 0: 1 + 1 + carry 0 = 2. Result digit = 0, carry = 1. Column 1: 1 + 0 + carry 1 = 2. Result digit = 0, carry = 1. No more digits but carry = 1, so append 1. Built array is [0, 0, 1]. Reverse to get "100".

Now try "1010" + "1011". Column 0: 0 + 1 + 0 = 1, digit 1, carry 0. Column 1: 1 + 1 + 0 = 2, digit 0, carry 1. Column 2: 0 + 0 + 1 = 1, digit 1, carry 0. Column 3: 1 + 1 + 0 = 2, digit 0, carry 1. Carry remains: digit 1. Built array is [1, 0, 1, 0, 1]. Reverse to get "10101".

Notice how the carry cascades exactly the way it does in decimal addition. The only difference is the modulus: binary uses 2 where decimal uses 10. This is why Add Binary and Add Two Numbers share the same skeleton.

⚠️

Common Mistake

Don't convert binary strings to integers, add, and convert back — this fails for very long strings that exceed integer limits. Process digit by digit with carry.

Edge Cases to Watch For

Different lengths are the most common source of bugs. When one string is shorter, your loop must treat missing digits as 0 rather than going out of bounds. The while loop condition that checks both pointers and the carry handles this naturally.

All ones input like "1111" + "1111" produces maximum carry propagation. The result is "11110", one digit longer than either input. Make sure your loop continues while carry is nonzero even after both pointers are exhausted.

The simplest edge case is "0" + "0" which should return "0". Your algorithm handles this correctly as long as you do not strip leading zeros from the output.

  • Different lengths: "1" + "1010" — shorter string needs zero-padding logic
  • Maximum carry: "1111" + "1111" = "11110"
  • Both zeros: "0" + "0" = "0"
  • One empty conceptually: the problem guarantees non-empty, but guard anyway

What Add Binary Teaches You

Add Binary is a carry propagation problem disguised as a string problem. Once you see the pattern, you can apply it to Add Two Numbers (#2) where the digits live in linked list nodes, to Plus One (#66) where you increment a number stored as an array, and to Multiply Strings (#43) where the arithmetic gets more complex but the digit-by-digit approach stays the same.

The key insight is that addition is addition regardless of the data structure holding the digits. Strings, arrays, linked lists — the carry logic never changes. Only the way you access each digit and store the result differs.

Practice this pattern with YeetCode flashcards to build instant recognition. When you see any problem that asks you to perform arithmetic on a non-integer representation, your first instinct should be right-to-left traversal with carry.

ℹ️

Pattern Connection

Add Binary (#67) uses the same carry pattern as Add Two Numbers (#2) and Plus One (#66) — master one and the other two are trivial. Binary just uses mod 2 instead of mod 10.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now