67. Add Binary - Explanation
Description
You are given two binary strings a and b, return their sum as a binary string.
Example 1:
Input: a = "101", b = "10"
Output: "111"Example 2:
Input: a = "10010", b = "111"
Output: "11001"Constraints:
1 <= a.length, b.length <= 10,000aandbconsist only of'0'or'1'characters.- Each string does not contain leading zeros except for the zero itself.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Binary Number System - Understanding how binary addition works with carry propagation
- String Manipulation - Iterating through strings, reversing, and building result strings character by character
- Two Pointers - Using two pointers to traverse strings of different lengths simultaneously from the end
1. Iteration
Intuition
Adding binary numbers works just like adding decimal numbers by hand, except we only have digits 0 and 1. We start from the rightmost digits (least significant bits) and add corresponding digits along with any carry from the previous position. If the sum is 2 or more, we carry 1 to the next position. We reverse both strings first to make indexing from the right easier, then build the result and reverse it at the end.
Algorithm
- Reverse both input strings for easier right-to-left processing.
- Initialize
carry = 0and an empty result string. - For each position from 0 to the maximum length of the two strings:
- Get the digit from each string (0 if past the string's length).
- Calculate
total = digitA + digitB + carry. - Append
total % 2to the result. - Update
carry = total / 2.
- If
carryremains, append "1". - Reverse the result string and return.
class Solution:
def addBinary(self, a: str, b: str) -> str:
res = ""
carry = 0
a, b = a[::-1], b[::-1]
for i in range(max(len(a), len(b))):
digitA = ord(a[i]) - ord("0") if i < len(a) else 0
digitB = ord(b[i]) - ord("0") if i < len(b) else 0
total = digitA + digitB + carry
char = str(total % 2)
res = char + res
carry = total // 2
if carry:
res = "1" + res
return resTime & Space Complexity
- Time complexity:
- Space complexity:
Where and are the lengths of the strings and respectively.
2. Iteration (Optimal)
Intuition
Instead of reversing the strings upfront, we can use two pointers starting at the end of each string and work backward. This avoids the extra space and time needed to reverse the input strings. We continue until both pointers have moved past the beginning of their strings and no carry remains. The result is built in reverse order, so we reverse it once at the end.
Algorithm
- Initialize pointers
iandjat the last index of stringsaandb. - Initialize
carry = 0and an empty result list. - While
i >= 0orj >= 0orcarry > 0:- Get the digit at position
iina(0 ifi < 0). - Get the digit at position
jinb(0 ifj < 0). - Calculate
total = digitA + digitB + carry. - Append
total % 2to the result. - Update
carry = total / 2. - Decrement both
iandj.
- Get the digit at position
- Reverse the result and return as a string.
class Solution:
def addBinary(self, a: str, b: str) -> str:
res = []
carry = 0
i, j = len(a) - 1, len(b) - 1
while i >= 0 or j >= 0 or carry > 0:
digitA = int(a[i]) if i >= 0 else 0
digitB = int(b[j]) if j >= 0 else 0
total = digitA + digitB + carry
res.append(total % 2)
carry = total // 2
i -= 1
j -= 1
res.reverse()
return ''.join(map(str, res))Time & Space Complexity
- Time complexity:
- Space complexity:
Where and are the lengths of the strings and respectively.
Common Pitfalls
Forgetting the Final Carry
After processing all digits, there may still be a carry of 1 that needs to be added to the result. Forgetting to check for this will produce incorrect results for cases like "1" + "1" = "10".
# Wrong: missing final carry check
return ''.join(res)
# Correct: handle remaining carry
if carry:
res.append('1')
return ''.join(res)Processing Strings in Wrong Direction
Binary addition must process digits from right to left (least significant to most significant). A common mistake is iterating from the start of the strings instead of the end, which produces completely wrong results.
Sign in to join the discussion