Leetcode – Add Binary Solution in Python

Spread the love

This article will walk you through the ‘Add Binary’ problem from LeetCode and discuss how to solve it using Python.

Problem Statement

The ‘Add Binary’ problem (LeetCode problem #67) states:

Given two binary strings a and b, return their sum as a binary string.

Here are a few examples:

  • Input: a = “11”, b = “1” Output: “100”
  • Input: a = “1010”, b = “1011” Output: “10101”


  • Each string is a non-empty binary string that won’t exceed 10^4 in length.

Understanding the Problem

The problem is asking to add two binary numbers. Binary numbers are based on powers of 2, just like decimal numbers are based on powers of 10. For example, in decimal numbers, the rightmost place represents 10^0, the next place to the left represents 10^1, then 10^2, and so on. In binary numbers, the rightmost place represents 2^0, the next place to the left represents 2^1, then 2^2, and so on.

To add binary numbers, we start from the rightmost bit (least significant bit) and move to the left, just like we do with decimal numbers. The only difference is that we carry over values to the next digit when the sum equals or exceeds 2 (not 10).

Approach to Solve the Problem

Let’s solve this problem step-by-step using Python:

Step 1: Import Necessary Libraries

In this problem, we don’t need any special libraries, so we can skip this step.

Step 2: Create a Function

We will create a function called addBinary that takes two binary strings as its parameters:

def addBinary(a, b):

Step 3: Main Logic

Python has built-in functions bin and int that can convert binary to integer and vice versa. We can use these functions to simplify our task:

def addBinary(a, b):
    # Convert binary strings to integers, add them, then convert back to binary
    return bin(int(a, 2) + int(b, 2))[2:]

Here’s what’s happening in this function:

  • int(a, 2) and int(b, 2) convert binary strings a and b to integers.
  • int(a, 2) + int(b, 2) adds these integers.
  • bin() then converts the sum back to a binary string. The binary string starts with ‘0b’, representing that it is a binary number.
  • [2:] removes the first two characters (‘0b’) from the binary string.

And that’s it! This function will return the sum of the two binary strings as a binary string.

Testing the Solution

Let’s test our solution with the sample inputs:

print(addBinary("11", "1")) # Output: "100"
print(addBinary("1010", "1011")) # Output: "10101"

And it works! Our function correctly sums the binary strings.


Solving problems on platforms like LeetCode is a great way to enhance your problem-solving skills and prepare for technical interviews. Although Python provides built-in functions that make this ‘Add Binary’ problem simpler, understanding the underlying logic of binary addition is crucial. This will help you solve more complex problems where such built-in functions may not be available. Keep practicing, and you’ll become more proficient in handling such problems!

Leave a Reply