Advertisement

Add Binary - LeetCode 67 Solution

Add Binary - Complete Solution Guide

Add Binary is LeetCode problem 67, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 <= 10 4 a and b consist only of '0' or '1' characters. Each string does not contain leading zeros except for the zero itself.

Detailed Explanation

At its heart, this problem asks us to perform binary addition, just like we learned decimal addition in school, but with only two digits: '0' and '1'. The catch? Our binary numbers are provided as strings, and they can be quite long – up to 10,000 characters! This immediately tells us we can't simply convert them to integers, add them, and convert back, as standard integer types (like Python's `int` or Java's `long`) would overflow for such massive numbers.

Solution Approach

The solution elegantly mirrors how we'd manually add binary numbers with pencil and paper. We iterate from the least significant bit (rightmost character) to the most significant bit (leftmost character), keeping track of a `carry`. For each position, we sum the bits from `a`, the bits from `b`, and the `carry` from the previous position. The current bit of our result is the sum modulo 2 (e.g., 0+0=0, 0+1=1, 1+1=0 with a carry, 1+1+1=1 with a carry). The new `carry` for the next position is the sum integer-divided by 2. This process continues until all bits are processed, and finally, any leftover `carry` is prepended to the result. Padding the shorter string with leading zeros (`zfill`) simplifies the loop by ensuring both strings have the same effective length, preventing out-of-bounds access and making the alignment straightforward.

Step-by-Step Algorithm

  1. Step 1: Initialize a carry variable to 0 and an empty string to store the result.
  2. Step 2: Iterate through the binary strings from right to left (least significant bit to most significant bit).
  3. Step 3: For each bit position, add the bits from both input strings (converted to integers) and the carry.
  4. Step 4: The remainder when the sum is divided by 2 represents the current bit of the result. Add this bit to the result string.
  5. Step 5: The quotient when the sum is divided by 2 is the new carry.
  6. Step 6: After the loop, if the carry is 1, add a leading '1' to the result.
  7. Step 7: Return the reversed result string (because we built the string from right to left).

Key Insights

  • Simulate manual binary addition: Iterate from right-to-left (least significant bit to most significant bit), just like paper-and-pencil arithmetic, calculating each sum and managing a carry-over.
  • Explicitly handle carry propagation: The core logic for each bit is `(digit_a + digit_b + carry_in) % 2` for the current result bit, and `(digit_a + digit_b + carry_in) // 2` for the `carry_out` to the next position.
  • Don't forget the final carry: After processing all corresponding digits, a lingering `carry` (if it's '1') must be prepended to the entire result, as seen in `"11" + "1" = "100"` where the '1' is the final carry.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Math, String, Bit Manipulation, Simulation.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Google, Meta, Microsoft, SIG, Uber, Walmart Labs, tcs.