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 <= 104a and b consist only of '0' or '1' characters.Problem Overview: You are given two binary strings a and b. Add them and return the result as another binary string. The challenge is performing binary addition manually since the inputs can be longer than typical integer limits.
Approach 1: Bit-by-Bit Addition with Carry (O(n) time, O(n) space)
This approach simulates how binary addition works on paper. Start from the rightmost digit of both strings and move left. At each step, add the current bits along with a carry. The sum determines the resulting bit (sum % 2) and the new carry (sum // 2). Continue until both strings are processed. If a carry remains at the end, append it to the result.
Use two pointers starting from the end of each string. If one string is shorter, treat missing digits as 0. Store resulting bits in a list or string builder and reverse at the end. This approach directly mirrors binary arithmetic and avoids integer overflow issues. Time complexity is O(n) where n is the length of the longer string. Space complexity is O(n) for the result string.
This solution relies on concepts from string traversal and bit manipulation. Many interviewers expect this approach because it demonstrates understanding of binary arithmetic rather than relying on built-in conversions.
Approach 2: Using Built-in Big Integer Libraries (O(n) time, O(n) space)
Another option converts the binary strings to integers using built-in language features, performs normal addition, and converts the result back to binary. In Python, use int(a, 2) and bin(sum). In JavaScript, BigInt can handle arbitrarily large numbers. Java provides BigInteger for the same purpose.
This approach is concise and easy to implement, but it relies on language-level big integer support. Internally, these libraries still perform bit operations with complexity proportional to the number of digits, giving roughly O(n) time and O(n) space. The tradeoff is less control over the algorithm and reduced demonstration of algorithmic reasoning.
The technique relates to math operations on different number bases. It works well in production code when readability matters more than demonstrating low-level logic.
Recommended for interviews: Bit-by-bit addition with carry is the expected solution. It shows that you understand binary arithmetic and can simulate operations using simple iteration and state management. The big integer approach is acceptable in real-world code but usually not the answer interviewers want first.
This method involves simulating the addition process as if one were adding numbers on paper. We traverse both strings from the least significant bit to the most, adding corresponding bits and maintaining a carry as needed. If the carry is still 1 after processing both strings, it is added to the result.
The code declares an array result to hold the final binary string, iterates through the input strings from the end to the start, calculates the bit sum while considering a carry, and builds the result string by adjusting the index with a carry sum. The final output is the result trimmed to exclude leading zeros.
C++
Java
Python
C#
JavaScript
Time Complexity: O(max(N, M)) where N and M are the lengths of strings a and b. The space complexity is also O(max(N, M)) to store the result.
This approach leverages built-in support for manipulating large integers available in many programming languages. By converting binary strings to integers, performing arithmetic operations, and then converting the result back to binary format, we can simplify the computation.
This Python solution uses the int function to convert binary strings to integers, adds them, and converts the sum back to binary format using the bin function, stripping the '0b' prefix with slicing.
Java
JavaScript
Time Complexity: O(N + M), due to integer conversion and addition operations. Space Complexity: O(N + M) for storing integer representations.
| Approach | Complexity |
|---|---|
| Bit-by-Bit Addition with Carry | Time Complexity: O(max(N, M)) where N and M are the lengths of strings |
| Using Built-in Big Integer Libraries | Time Complexity: O(N + M), due to integer conversion and addition operations. Space Complexity: O(N + M) for storing integer representations. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Bit-by-Bit Addition with Carry | O(n) | O(n) | Best for interviews and when implementing binary addition manually |
| Built-in Big Integer Conversion | O(n) | O(n) | When the language provides big integer utilities and code simplicity is preferred |
Add Binary - Leetcode 67 - Python • NeetCode • 73,772 views views
Watch 9 more video solutions →Practice Add Binary with our built-in code editor and test cases.
Practice on FleetCode